Python 3.6.15 (default, Dec 3 2021, 18:25:24) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.

In [ ]:
#imports
# from skimage.filters import threshold_local
# import argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import imutils
from imutils import perspective
from imutils import contours
In [ ]:
pixelsPerMetric = 60
In [ ]:
def midpoint(ptA, ptB): # to use in below function
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
In [ ]:
# # construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()


# ap.add_argument("-i", "--image", type=str, default="IMG_1097.JPG",
# 	help="path to input image")
# ap.add_argument("-w", "--width", type=float, default=10.0,
# 	help="width of the left-most object in the image (in inches)")

# args = vars(ap.parse_args())
In [ ]:
%matplotlib inline

# load the input image
# image = cv2.imread(args["image"]) 
# image = cv2.imread("frame126.jpg")

image = cv2.imread("IMG_1097.JPG")
image = imutils.resize(image, height = 500) # resize
oorig = image.copy()
plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
Out[ ]:
<matplotlib.image.AxesImage at 0x142826b2e10>
In [ ]:
# Select ROI dynamically in real time

# r = cv2.selectROI(image)
# cv2.waitKey(0)

# # Crop image
# roi = image[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# a = int(r[1])
# b = int(r[1]+r[3])
# c = int(r[0])
# d = int(r[0]+r[2])
# print(f"{a}:{b}, {c}:{d}")
# cv2.waitKey(0)
# cv2.imshow("Cropped ROI", roi)
# cv2.destroyAllWindows()


# y1=a
# y2=b
# x1=c
# x2=d
# # %%
# cv2.destroyAllWindows()
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428260fda0>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
Out[ ]:
<matplotlib.image.AxesImage at 0x14282f05a90>
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
tc # tile contours with rectangle border
Out[ ]:
[array([[224, 324],
        [491, 338],
        [488, 385],
        [222, 371]]),
 array([[231, 382],
        [479, 390],
        [478, 434],
        [229, 426]])]
In [ ]:
counter = 0

# image_tile = oorig
for t in tc:
	# print(c)
	image_tile = oorig
	roi_tile = image_tile[t[0][1]:t[2][1], t[0][0]:t[2][0]]

	cv2.imshow("roi_tile", roi_tile)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

	gray = cv2.cvtColor(roi_tile, cv2.COLOR_BGR2GRAY)
	# plt.imshow(imutils.opencv2matplotlib(gray))
	# cv2.imshow("Gray", gray)
	# plt.imshow(imutils.opencv2matplotlib(image))
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()

	# # %%

	# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
	# reducing high frequency noise
	blurred = cv2.GaussianBlur(gray, (11, 11), 0)
	# plt.imshow(imutils.opencv2matplotlib(blurred))



	# # %%

	# applying edge detection
	# edged = cv2.Canny(gray, 200, 255) 
	# edged = cv2.Canny(blurred, 100, 200) # blurred
	edged = cv2.Canny(blurred, 50, 100) # blurred
	# plt.imshow(imutils.opencv2matplotlib(edged))


	# # %%
	# # Autocanny 
	# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
	# edgeMap = imutils.auto_canny(blurred)
	# # cv2.imshow("Original", image)
	# plt.imshow(imutils.opencv2matplotlib(edgeMap))


	# # %%

	dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
	eroded = cv2.erode(dilated, None, iterations=2)
	# plt.imshow(imutils.opencv2matplotlib(eroded))


	# # %%

	# threshold the image by setting all pixel values less than 225 to 255 
	# (white; foreground) and all pixel values >= 225 to 255
	# (black; background), thereby segmenting the image 
	# in short binary inversion
	thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
	# plt.imshow(imutils.opencv2matplotlib(thresh))

	# # %%
	# we apply erosions to reduce the size of foreground objects
	# further erosion to lose some unwanted small spaces
	mask = thresh.copy()
	mask = cv2.erode(mask, None, iterations=1)
	# plt.imshow(imutils.opencv2matplotlib(mask))


	# # %%
	# find contours in the edge map
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	cnts = imutils.grab_contours(cnts)

	# sort the contours from left-to-right and initialize the
	# 'pixels per metric' calibration variable
	(cnts, _) = contours.sort_contours(cnts)

	orig = oorig.copy()
	countertemp = 0
	for c in cnts:
		
		# orig = oorig.copy()
		# if the contour is not sufficiently large, ignore it
		# print("Area of contour ", cv2.contourArea(c))
		if cv2.contourArea(c) < 100: # or cv2.contourArea(c) > 4000:
			continue
		
		# compute the rotated bounding box of the contour
		box = cv2.minAreaRect(c)
		box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
		box = np.array(box, dtype="int")
		# print(box)
	
		# order the points in the contour such that they appear
		# in top-left, top-right, bottom-right, and bottom-left
		# order, then draw the outline of the rotated bounding box
	
		# box = perspective.order_points(box)
		# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
	#or
		box = perspective.order_points(box)
		box = box.astype("int")+[t[0][0],t[0][1]] #[x1,y1]
		# asd
		# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
	
		# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
		# roic = [
		# 	[x1,y1],
		# 	[x2,y1],
		# 	[x2,y2],
		# 	[x1,y2]
		# 	]
		# roic = np.array(roic, dtype="int")
		# print(roic)
	
		cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
		# cv2.drawContours(orig, [box.astype("int")], -1, (255, 255, 0), 2)
		countertemp += 1
		# print("countertemp", countertemp)
	
		# print(box.astype("int"))
		# print(box.astype("int")+[x1, y1])
	
		# loop over the original points and draw them
		for (x, y) in box:
			cv2.circle(orig, (int(x), int(y)), 3, (0, 0, 255), -1)
	
		# unpack the ordered bounding box, then compute the midpoint
		# between the top-left and top-right coordinates, followed by
		# the midpoint between bottom-left and bottom-right coordinates
		(tl, tr, br, bl) = box
		(tltrX, tltrY) = midpoint(tl, tr)
		(blbrX, blbrY) = midpoint(bl, br)
		# compute the midpoint between the top-left and top-right points,
		# followed by the midpoint between the top-righ and bottom-right
		(tlblX, tlblY) = midpoint(tl, bl)
		(trbrX, trbrY) = midpoint(tr, br)

		cv2.line(orig, tl, bl, (0, 0, 255), 1) 
		cv2.line(orig, tr, br, (0, 0, 255), 1)

		# for approxa in abcde:
		# 	for popints in range(len(approxa//2)):
		# 		print(approxa[popints], approxa[-popints])
		# 		cv2.line(orig, approxa[popints], approxa[-popints], (255, 0, 0), 1)

	
		# # draw the midpoints on the image
		# cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
	
		# # draw lines between the midpoints
		# cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
		# 	(255, 0, 255), 2)
		# cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
		# 	(255, 0, 255), 2)
	
		# compute the Euclidean distance between the midpoints
		dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
		dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
	
		# if the pixels per metric has not been initialized, then
		# compute it as the ratio of pixels to supplied metric
		# (in this case, inches)
		if pixelsPerMetric is None:
			pixelsPerMetric = dB / 10 # args["width"]
			# print("pixelsPerMetric :",pixelsPerMetric)

	
		# compute the size of the object
		dimA = dA / pixelsPerMetric
		dimB = dB / pixelsPerMetric

	
		# draw the object sizes on the image
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tl[0])-15, int(tl[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tr[0])-15, int(tr[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		# cv2.putText(orig, "{:.2f}in".format(dimB),
		# 	(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
		# 	0.35, (0, 0, 255), 1)
	
		# show the output image
		# plt.imshow(imutils.opencv2matplotlib(orig))
		cv2.imshow("Imager", orig)
		# print("Area of contour ", cv2.contourArea(c))
		counter += 1
		# print(counter)
		cv2.waitKey(0)

# mod abcde
	orig = oorig.copy()

	rabcde = abcde
	for approxa in abcde:
		for ppopints in range(1,len(approxa),2):
			approxa[-ppopints][0][0]=approxa[ppopints][0][0]


# abcde
	approxac=0
	for approxa in abcde:
		for popints in range(1,len(approxa)//2):
			# print(approxa[popints], approxa[-popints])
			# print(popints)
			# print("len(approxa//2:",len(approxa)//2)
			# dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
			dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
			dimA = dA / pixelsPerMetric

			cv2.line(orig, approxa[popints][0]+[x1,y1], approxa[-popints][0]+[x1,y1], (0, 255, 0), 1)
			cv2.circle(orig, approxa[popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			# if popints!=1:
				# continue
			cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10+4),
						# (int(tl[0])-15, int(tl[1])-15), 
						approxa[popints][0]+[x1,y1], 
						cv2.FONT_HERSHEY_SIMPLEX,
						0.35, (0, 255, 0), 1)
		approxac+=1

	cv2.imshow("Imager", orig)
	cv2.waitKey(0)
	
	cv2.destroyAllWindows()
In [ ]:
	cv2.destroyAllWindows()




###############################################################
# working on better contours
# plan to get local minimas and maximas
In [ ]:
# # for viewing all contours / debugging
# image2=roi
# contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
In [ ]:
# # for viewing all contours / debugging
image2=roi.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x142833271d0>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(tcai))
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
#imports
# from skimage.filters import threshold_local
# import argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import imutils
from imutils import perspective
from imutils import contours
In [ ]:
pixelsPerMetric = 60
In [ ]:
def midpoint(ptA, ptB): # to use in below function
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
In [ ]:
# # construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()


# ap.add_argument("-i", "--image", type=str, default="IMG_1097.JPG",
# 	help="path to input image")
# ap.add_argument("-w", "--width", type=float, default=10.0,
# 	help="width of the left-most object in the image (in inches)")

# args = vars(ap.parse_args())
In [ ]:
%matplotlib inline

# load the input image
# image = cv2.imread(args["image"]) 
# image = cv2.imread("frame126.jpg")

image = cv2.imread("IMG_1097.JPG")
image = imutils.resize(image, height = 500) # resize
oorig = image.copy()
plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
Out[ ]:
<matplotlib.image.AxesImage at 0x14282fcc358>
In [ ]:
# Select ROI dynamically in real time

# r = cv2.selectROI(image)
# cv2.waitKey(0)

# # Crop image
# roi = image[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# a = int(r[1])
# b = int(r[1]+r[3])
# c = int(r[0])
# d = int(r[0]+r[2])
# print(f"{a}:{b}, {c}:{d}")
# cv2.waitKey(0)
# cv2.imshow("Cropped ROI", roi)
# cv2.destroyAllWindows()


# y1=a
# y2=b
# x1=c
# x2=d
# # %%
# cv2.destroyAllWindows()
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428233d2b0>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
Out[ ]:
<matplotlib.image.AxesImage at 0x142823a6f60>
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
tc # tile contours with rectangle border
Out[ ]:
[array([[224, 324],
        [491, 338],
        [488, 385],
        [222, 371]]),
 array([[231, 382],
        [479, 390],
        [478, 434],
        [229, 426]])]
In [ ]:
counter = 0

# image_tile = oorig
for t in tc:
	# print(c)
	image_tile = oorig
	roi_tile = image_tile[t[0][1]:t[2][1], t[0][0]:t[2][0]]

	cv2.imshow("roi_tile", roi_tile)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

	gray = cv2.cvtColor(roi_tile, cv2.COLOR_BGR2GRAY)
	# plt.imshow(imutils.opencv2matplotlib(gray))
	# cv2.imshow("Gray", gray)
	# plt.imshow(imutils.opencv2matplotlib(image))
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()

	# # %%

	# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
	# reducing high frequency noise
	blurred = cv2.GaussianBlur(gray, (11, 11), 0)
	# plt.imshow(imutils.opencv2matplotlib(blurred))



	# # %%

	# applying edge detection
	# edged = cv2.Canny(gray, 200, 255) 
	# edged = cv2.Canny(blurred, 100, 200) # blurred
	edged = cv2.Canny(blurred, 50, 100) # blurred
	# plt.imshow(imutils.opencv2matplotlib(edged))


	# # %%
	# # Autocanny 
	# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
	# edgeMap = imutils.auto_canny(blurred)
	# # cv2.imshow("Original", image)
	# plt.imshow(imutils.opencv2matplotlib(edgeMap))


	# # %%

	dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
	eroded = cv2.erode(dilated, None, iterations=2)
	# plt.imshow(imutils.opencv2matplotlib(eroded))


	# # %%

	# threshold the image by setting all pixel values less than 225 to 255 
	# (white; foreground) and all pixel values >= 225 to 255
	# (black; background), thereby segmenting the image 
	# in short binary inversion
	thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
	# plt.imshow(imutils.opencv2matplotlib(thresh))

	# # %%
	# we apply erosions to reduce the size of foreground objects
	# further erosion to lose some unwanted small spaces
	mask = thresh.copy()
	mask = cv2.erode(mask, None, iterations=1)
	# plt.imshow(imutils.opencv2matplotlib(mask))


	# # %%
	# find contours in the edge map
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	cnts = imutils.grab_contours(cnts)

	# sort the contours from left-to-right and initialize the
	# 'pixels per metric' calibration variable
	(cnts, _) = contours.sort_contours(cnts)

	orig = oorig.copy()
	countertemp = 0
	for c in cnts:
		
		# orig = oorig.copy()
		# if the contour is not sufficiently large, ignore it
		# print("Area of contour ", cv2.contourArea(c))
		if cv2.contourArea(c) < 100: # or cv2.contourArea(c) > 4000:
			continue
		
		# compute the rotated bounding box of the contour
		box = cv2.minAreaRect(c)
		box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
		box = np.array(box, dtype="int")
		# print(box)
	
		# order the points in the contour such that they appear
		# in top-left, top-right, bottom-right, and bottom-left
		# order, then draw the outline of the rotated bounding box
	
		# box = perspective.order_points(box)
		# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
	#or
		box = perspective.order_points(box)
		box = box.astype("int")+[t[0][0],t[0][1]] #[x1,y1]
		# asd
		# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
	
		# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
		# roic = [
		# 	[x1,y1],
		# 	[x2,y1],
		# 	[x2,y2],
		# 	[x1,y2]
		# 	]
		# roic = np.array(roic, dtype="int")
		# print(roic)
	
		cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
		# cv2.drawContours(orig, [box.astype("int")], -1, (255, 255, 0), 2)
		countertemp += 1
		# print("countertemp", countertemp)
	
		# print(box.astype("int"))
		# print(box.astype("int")+[x1, y1])
	
		# loop over the original points and draw them
		for (x, y) in box:
			cv2.circle(orig, (int(x), int(y)), 3, (0, 0, 255), -1)
	
		# unpack the ordered bounding box, then compute the midpoint
		# between the top-left and top-right coordinates, followed by
		# the midpoint between bottom-left and bottom-right coordinates
		(tl, tr, br, bl) = box
		(tltrX, tltrY) = midpoint(tl, tr)
		(blbrX, blbrY) = midpoint(bl, br)
		# compute the midpoint between the top-left and top-right points,
		# followed by the midpoint between the top-righ and bottom-right
		(tlblX, tlblY) = midpoint(tl, bl)
		(trbrX, trbrY) = midpoint(tr, br)

		cv2.line(orig, tl, bl, (0, 0, 255), 1) 
		cv2.line(orig, tr, br, (0, 0, 255), 1)

		# for approxa in abcde:
		# 	for popints in range(len(approxa//2)):
		# 		print(approxa[popints], approxa[-popints])
		# 		cv2.line(orig, approxa[popints], approxa[-popints], (255, 0, 0), 1)

	
		# # draw the midpoints on the image
		# cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
	
		# # draw lines between the midpoints
		# cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
		# 	(255, 0, 255), 2)
		# cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
		# 	(255, 0, 255), 2)
	
		# compute the Euclidean distance between the midpoints
		dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
		dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
	
		# if the pixels per metric has not been initialized, then
		# compute it as the ratio of pixels to supplied metric
		# (in this case, inches)
		if pixelsPerMetric is None:
			pixelsPerMetric = dB / 10 # args["width"]
			# print("pixelsPerMetric :",pixelsPerMetric)

	
		# compute the size of the object
		dimA = dA / pixelsPerMetric
		dimB = dB / pixelsPerMetric

	
		# draw the object sizes on the image
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tl[0])-15, int(tl[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tr[0])-15, int(tr[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		# cv2.putText(orig, "{:.2f}in".format(dimB),
		# 	(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
		# 	0.35, (0, 0, 255), 1)
	
		# show the output image
		# plt.imshow(imutils.opencv2matplotlib(orig))
		cv2.imshow("Imager", orig)
		# print("Area of contour ", cv2.contourArea(c))
		counter += 1
		# print(counter)
		cv2.waitKey(0)

# mod abcde
	orig = oorig.copy()

	rabcde = abcde
	for approxa in abcde:
		for ppopints in range(1,len(approxa),2):
			approxa[-ppopints][0][0]=approxa[ppopints][0][0]


# abcde
	approxac=0
	for approxa in abcde:
		for popints in range(1,len(approxa)//2):
			# print(approxa[popints], approxa[-popints])
			# print(popints)
			# print("len(approxa//2:",len(approxa)//2)
			# dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
			dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
			dimA = dA / pixelsPerMetric

			cv2.line(orig, approxa[popints][0]+[x1,y1], approxa[-popints][0]+[x1,y1], (0, 255, 0), 1)
			cv2.circle(orig, approxa[popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			# if popints!=1:
				# continue
			cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10+4),
						# (int(tl[0])-15, int(tl[1])-15), 
						approxa[popints][0]+[x1,y1], 
						cv2.FONT_HERSHEY_SIMPLEX,
						0.35, (0, 255, 0), 1)
		approxac+=1

	cv2.imshow("Imager", orig)
	cv2.waitKey(0)
	
	cv2.destroyAllWindows()
In [ ]:
	cv2.destroyAllWindows()




###############################################################
# working on better contours
# plan to get local minimas and maximas
In [ ]:
# # for viewing all contours / debugging
# image2=roi
# contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
In [ ]:
# # for viewing all contours / debugging
image2=roi.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283017cf8>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(tcai))
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
bu
Out[ ]:
[array([ 58, 110], dtype=int32), array([ 55, 159], dtype=int32)]
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
Out[ ]:
<matplotlib.image.AxesImage at 0x14282466e80>
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric

	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(n*t),y1+15], pd[0]+[x1-(n*t),y1-25], (0, 0, 255), 1)
	cv2.line(orig, pu[0]+[x1+(n*t),y1+15], pd[0]+[x1+(n*t),y1-25], (255, 0, 0), 1)
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)

	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
cv2.imshow("iamgljler",orig)
In [ ]:
cv2.destroyAllWindows()

# cutout(bu, bd)
In [ ]:
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric

	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(n*t),y1+15], pd[0]+[x1-(n*t),y1-25], (0, 0, 255), 1)
	cv2.line(orig, pu[0]+[x1+(n*t),y1+15], pd[0]+[x1+(n*t),y1-25], (255, 0, 0), 1)
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)

	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x14282531358>
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(n*t),y1+15], pd[0]+[x1-(n*t),y1-25], (0, 0, 255), 1)
	cv2.line(orig, pu[0]+[x1+(n*t),y1+15], pd[0]+[x1+(n*t),y1-25], (255, 0, 0), 1)
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x142833604e0>
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1)
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1)
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428237e470>
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
plt.imshow(edged)
Out[ ]:
<matplotlib.image.AxesImage at 0x14283380b38>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(edged))
Out[ ]:
<matplotlib.image.AxesImage at 0x142824e6fd0>
In [ ]:
pd[0]
Out[ ]:
array([178,  96], dtype=int32)
In [ ]:
pu[0]+[x1-(i*t),y1+15]
Out[ ]:
array([374, 395])
In [ ]:
x1
Out[ ]:
214
In [ ]:
y1
Out[ ]:
263
In [ ]:
#imports
# from skimage.filters import threshold_local
# import argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import imutils
from imutils import perspective
from imutils import contours
In [ ]:
pixelsPerMetric = 60
In [ ]:
def midpoint(ptA, ptB): # to use in below function
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
In [ ]:
# # construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()


# ap.add_argument("-i", "--image", type=str, default="IMG_1097.JPG",
# 	help="path to input image")
# ap.add_argument("-w", "--width", type=float, default=10.0,
# 	help="width of the left-most object in the image (in inches)")

# args = vars(ap.parse_args())
In [ ]:
%matplotlib inline

# load the input image
# image = cv2.imread(args["image"]) 
# image = cv2.imread("frame126.jpg")

image = cv2.imread("IMG_1097.JPG")
image = imutils.resize(image, height = 500) # resize
oorig = image.copy()
plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428411ab00>
In [ ]:
# Select ROI dynamically in real time

# r = cv2.selectROI(image)
# cv2.waitKey(0)

# # Crop image
# roi = image[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# a = int(r[1])
# b = int(r[1]+r[3])
# c = int(r[0])
# d = int(r[0]+r[2])
# print(f"{a}:{b}, {c}:{d}")
# cv2.waitKey(0)
# cv2.imshow("Cropped ROI", roi)
# cv2.destroyAllWindows()


# y1=a
# y2=b
# x1=c
# x2=d
# # %%
# cv2.destroyAllWindows()
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428417fa58>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
Out[ ]:
<matplotlib.image.AxesImage at 0x14283cb1d30>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
# mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
Out[ ]:
<matplotlib.image.AxesImage at 0x14283d25860>
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
Out[ ]:
<matplotlib.image.AxesImage at 0x142859f1ba8>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))
mask3 = mask.copy()


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
In [ ]:
maskt = mask3.copy()
cv2.line(maskt, coordsr[0], coordsr[0]+[1,0], (0, 0, 255), 1) # L
plt.imshow(imutils.opencv2matplotlib(maskt))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=729'>730</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=730'>731</a> maskt = mask3.copy()
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=731'>732</a> cv2.line(maskt, coordsr[0], coordsr[0]+[1,0], (0, 0, 255), 1) # L
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=732'>733</a> plt.imshow(imutils.opencv2matplotlib(maskt))
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=733'>734</a> 

error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'line'
> Overload resolution failed:
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
maskt = mask3.copy()
cv2.line(maskt, coordsr[0][0], coordsr[0]+[1], (0, 0, 255), 1) # L
plt.imshow(imutils.opencv2matplotlib(maskt))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=729'>730</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=730'>731</a> maskt = mask3.copy()
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=731'>732</a> cv2.line(maskt, coordsr[0][0], coordsr[0]+[1], (0, 0, 255), 1) # L
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=732'>733</a> plt.imshow(imutils.opencv2matplotlib(maskt))
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=733'>734</a> 

error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'line'
> Overload resolution failed:
>  - Can't parse 'pt2'. Expected sequence length 2, got 3
>  - Can't parse 'pt2'. Expected sequence length 2, got 3
In [ ]:
coordsr[0]+[1]
Out[ ]:
[array([392, 395]), array([392, 334]), 1]
In [ ]:
maskt = mask3.copy()
cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1) # L
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a634080>
In [ ]:
coordsr[0][1]
Out[ ]:
array([392, 334])
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	# coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	# coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
maskt = mask3.copy()
cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1) # L
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a678d68>
In [ ]:
maskt = mask3.copy()
cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a70f0b8>
In [ ]:
coordsr[0][0]
Out[ ]:
array([178, 132])
In [ ]:
coordsr[0][1]
Out[ ]:
array([178,  71])
In [ ]:
y1=coordsr[0][0][1]
y2=coordsr[0][1][1]
x1=coordsr[0][0]
x2=coordsr[0][0]+[1,0]
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=743'>744</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=744'>745</a> roib = maskt[y1:y2, x1:x2]
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=745'>746</a> # roi = image[207:479, 214:494]
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=746'>747</a> # roi = image[263:433, 214:491]
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=747'>748</a> # roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]

TypeError: only integer scalar arrays can be converted to a scalar index
In [ ]:
coordsr[0][0][1]
Out[ ]:
132
In [ ]:
coordsr[0][1][1]
Out[ ]:
71
In [ ]:
coordsr[0][0]
Out[ ]:
array([178, 132])
In [ ]:
coordsr[0][0][0]+1
Out[ ]:
179
In [ ]:
coordsr[0][0][0]
Out[ ]:
178
In [ ]:
y1=coordsr[0][0][1]
y2=coordsr[0][1][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+1
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
roib
Out[ ]:
array([], shape=(0, 1), dtype=uint8)
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=748'>749</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=749'>750</a> plt.imshow(imutils.opencv2matplotlib(roib))

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
y1=coordsr[0][0][1]
y2=coordsr[0][1][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+2
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
roib
Out[ ]:
array([], shape=(0, 2), dtype=uint8)
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=748'>749</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=749'>750</a> plt.imshow(imutils.opencv2matplotlib(roib))

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
maskt
Out[ ]:
array([[255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       ...,
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255]], dtype=uint8)
In [ ]:
coordsr[0][0][1]
Out[ ]:
132
In [ ]:
coordsr[0][1][1]
Out[ ]:
71
In [ ]:
coordsr[0][0][0]
Out[ ]:
178
In [ ]:
coordsr[0][0][0]+2
Out[ ]:
180
In [ ]:
y1=coordsr[0][0][1]
y2=coordsr[0][1][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+2
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=748'>749</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=749'>750</a> plt.imshow(imutils.opencv2matplotlib(roib))

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
roib = maskt[y2:y1, x2:x1]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=748'>749</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=749'>750</a> plt.imshow(imutils.opencv2matplotlib(roib))

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
maskt
Out[ ]:
array([[255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       ...,
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255]], dtype=uint8)
In [ ]:
maskt.shape()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 maskt.shape()

TypeError: 'tuple' object is not callable
In [ ]:
maskt.shape
Out[ ]:
(170, 277)
In [ ]:
y1=coordsr[0][0][1]
y2=coordsr[0][1][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+2
In [ ]:
y1
Out[ ]:
132
In [ ]:
y2
Out[ ]:
71
In [ ]:
x2
Out[ ]:
180
In [ ]:
x1
Out[ ]:
178
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+2
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a7b70b8>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+5
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a815b38>
In [ ]:
maskt
Out[ ]:
array([[255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       ...,
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255]], dtype=uint8)
In [ ]:
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x14282501160>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+20
In [ ]:
roib = maskt[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a6d60b8>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+20
In [ ]:
roib = maskt[y1:y2, x1:x2]
cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
Out[ ]:
array([[255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       [255, 255,   0, ..., 255, 255, 255],
       ...,
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255],
       [  0,   0,   0, ..., 255, 255, 255]], dtype=uint8)
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283ce7be0>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a907b38>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+200
In [ ]:
roib = maskt[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a97a898>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428a9de320>
In [ ]:
maskt = mask3.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428aa3af60>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+200
In [ ]:
roib = maskt[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428aaa1b00>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+2
In [ ]:
roib = maskt[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ab02668>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+1
In [ ]:
roib = maskt[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ab6c048>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(eroi))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428abde630>
In [ ]:
# # for viewing all contours / debugging
image2=maskt.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ac22978>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428acc46a0>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+5
In [ ]:
roib = maskt[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(roib))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ad29710>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ad83a20>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428adeb7b8>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ae45a20>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428be92780>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, 1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428bee7f98>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, 1, (255,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428bf4d940>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, 1, (255,255,255),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x142852fdac8>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, 1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ac48160>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(eroi))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428adc20f0>
In [ ]:
# # for viewing all contours / debugging
image2=roib.copy()
contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, 1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c0c4b38>
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
slice = oorig.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c12dbe0>
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
origs = oorig.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c253d68>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+5
In [ ]:
slice = origs[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c138cf8>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+500
In [ ]:
slice = origs[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c19c0f0>
In [ ]:
plt.imshow(imutils.opencv2matplotlib(maskt))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c1fd080>
In [ ]:
origs = roi.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c2e7dd8>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+500
In [ ]:
slice = origs[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=749'>750</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=750'>751</a> plt.imshow(imutils.opencv2matplotlib(slice))
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=751'>752</a> 

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1) # L
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1) # R
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c632550>
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=660'>661</a> # dimA = dA / pixelsPerMetric
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=661'>662</a> 
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=662'>663</a> cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=663'>664</a> cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=664'>665</a> cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

IndexError: list index out of range
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=660'>661</a> # dimA = dA / pixelsPerMetric
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=661'>662</a> 
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=662'>663</a> cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=663'>664</a> cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=664'>665</a> cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

IndexError: list index out of range
In [ ]:
#imports
# from skimage.filters import threshold_local
# import argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import imutils
from imutils import perspective
from imutils import contours
In [ ]:
pixelsPerMetric = 60
In [ ]:
def midpoint(ptA, ptB): # to use in below function
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
In [ ]:
# # construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()


# ap.add_argument("-i", "--image", type=str, default="IMG_1097.JPG",
# 	help="path to input image")
# ap.add_argument("-w", "--width", type=float, default=10.0,
# 	help="width of the left-most object in the image (in inches)")

# args = vars(ap.parse_args())
In [ ]:
%matplotlib inline

# load the input image
# image = cv2.imread(args["image"]) 
# image = cv2.imread("frame126.jpg")

image = cv2.imread("IMG_1097.JPG")
image = imutils.resize(image, height = 500) # resize
oorig = image.copy()
plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
Out[ ]:
<matplotlib.image.AxesImage at 0x14285c8eba8>
In [ ]:
# Select ROI dynamically in real time

# r = cv2.selectROI(image)
# cv2.waitKey(0)

# # Crop image
# roi = image[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# a = int(r[1])
# b = int(r[1]+r[3])
# c = int(r[0])
# d = int(r[0]+r[2])
# print(f"{a}:{b}, {c}:{d}")
# cv2.waitKey(0)
# cv2.imshow("Cropped ROI", roi)
# cv2.destroyAllWindows()


# y1=a
# y2=b
# x1=c
# x2=d
# # %%
# cv2.destroyAllWindows()
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428541db70>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))
mask3 = mask.copy()


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
tc # tile contours with rectangle border
Out[ ]:
[array([[224, 324],
        [491, 338],
        [488, 385],
        [222, 371]]),
 array([[231, 382],
        [479, 390],
        [478, 434],
        [229, 426]])]
In [ ]:
counter = 0

# image_tile = oorig
for t in tc:
	# print(c)
	image_tile = oorig
	roi_tile = image_tile[t[0][1]:t[2][1], t[0][0]:t[2][0]]

	cv2.imshow("roi_tile", roi_tile)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

	gray = cv2.cvtColor(roi_tile, cv2.COLOR_BGR2GRAY)
	# plt.imshow(imutils.opencv2matplotlib(gray))
	# cv2.imshow("Gray", gray)
	# plt.imshow(imutils.opencv2matplotlib(image))
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()

	# # %%

	# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
	# reducing high frequency noise
	blurred = cv2.GaussianBlur(gray, (11, 11), 0)
	# plt.imshow(imutils.opencv2matplotlib(blurred))



	# # %%

	# applying edge detection
	# edged = cv2.Canny(gray, 200, 255) 
	# edged = cv2.Canny(blurred, 100, 200) # blurred
	edged = cv2.Canny(blurred, 50, 100) # blurred
	# plt.imshow(imutils.opencv2matplotlib(edged))


	# # %%
	# # Autocanny 
	# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
	# edgeMap = imutils.auto_canny(blurred)
	# # cv2.imshow("Original", image)
	# plt.imshow(imutils.opencv2matplotlib(edgeMap))


	# # %%

	dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
	eroded = cv2.erode(dilated, None, iterations=2)
	# plt.imshow(imutils.opencv2matplotlib(eroded))


	# # %%

	# threshold the image by setting all pixel values less than 225 to 255 
	# (white; foreground) and all pixel values >= 225 to 255
	# (black; background), thereby segmenting the image 
	# in short binary inversion
	thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
	# plt.imshow(imutils.opencv2matplotlib(thresh))

	# # %%
	# we apply erosions to reduce the size of foreground objects
	# further erosion to lose some unwanted small spaces
	mask = thresh.copy()
	mask = cv2.erode(mask, None, iterations=1)
	# plt.imshow(imutils.opencv2matplotlib(mask))


	# # %%
	# find contours in the edge map
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	cnts = imutils.grab_contours(cnts)

	# sort the contours from left-to-right and initialize the
	# 'pixels per metric' calibration variable
	(cnts, _) = contours.sort_contours(cnts)

	orig = oorig.copy()
	countertemp = 0
	for c in cnts:
		
		# orig = oorig.copy()
		# if the contour is not sufficiently large, ignore it
		# print("Area of contour ", cv2.contourArea(c))
		if cv2.contourArea(c) < 100: # or cv2.contourArea(c) > 4000:
			continue
		
		# compute the rotated bounding box of the contour
		box = cv2.minAreaRect(c)
		box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
		box = np.array(box, dtype="int")
		# print(box)
	
		# order the points in the contour such that they appear
		# in top-left, top-right, bottom-right, and bottom-left
		# order, then draw the outline of the rotated bounding box
	
		# box = perspective.order_points(box)
		# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
	#or
		box = perspective.order_points(box)
		box = box.astype("int")+[t[0][0],t[0][1]] #[x1,y1]
		# asd
		# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
	
		# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
		# roic = [
		# 	[x1,y1],
		# 	[x2,y1],
		# 	[x2,y2],
		# 	[x1,y2]
		# 	]
		# roic = np.array(roic, dtype="int")
		# print(roic)
	
		cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
		# cv2.drawContours(orig, [box.astype("int")], -1, (255, 255, 0), 2)
		countertemp += 1
		# print("countertemp", countertemp)
	
		# print(box.astype("int"))
		# print(box.astype("int")+[x1, y1])
	
		# loop over the original points and draw them
		for (x, y) in box:
			cv2.circle(orig, (int(x), int(y)), 3, (0, 0, 255), -1)
	
		# unpack the ordered bounding box, then compute the midpoint
		# between the top-left and top-right coordinates, followed by
		# the midpoint between bottom-left and bottom-right coordinates
		(tl, tr, br, bl) = box
		(tltrX, tltrY) = midpoint(tl, tr)
		(blbrX, blbrY) = midpoint(bl, br)
		# compute the midpoint between the top-left and top-right points,
		# followed by the midpoint between the top-righ and bottom-right
		(tlblX, tlblY) = midpoint(tl, bl)
		(trbrX, trbrY) = midpoint(tr, br)

		cv2.line(orig, tl, bl, (0, 0, 255), 1) 
		cv2.line(orig, tr, br, (0, 0, 255), 1)

		# for approxa in abcde:
		# 	for popints in range(len(approxa//2)):
		# 		print(approxa[popints], approxa[-popints])
		# 		cv2.line(orig, approxa[popints], approxa[-popints], (255, 0, 0), 1)

	
		# # draw the midpoints on the image
		# cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
	
		# # draw lines between the midpoints
		# cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
		# 	(255, 0, 255), 2)
		# cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
		# 	(255, 0, 255), 2)
	
		# compute the Euclidean distance between the midpoints
		dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
		dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
	
		# if the pixels per metric has not been initialized, then
		# compute it as the ratio of pixels to supplied metric
		# (in this case, inches)
		if pixelsPerMetric is None:
			pixelsPerMetric = dB / 10 # args["width"]
			# print("pixelsPerMetric :",pixelsPerMetric)

	
		# compute the size of the object
		dimA = dA / pixelsPerMetric
		dimB = dB / pixelsPerMetric

	
		# draw the object sizes on the image
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tl[0])-15, int(tl[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tr[0])-15, int(tr[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		# cv2.putText(orig, "{:.2f}in".format(dimB),
		# 	(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
		# 	0.35, (0, 0, 255), 1)
	
		# show the output image
		# plt.imshow(imutils.opencv2matplotlib(orig))
		cv2.imshow("Imager", orig)
		# print("Area of contour ", cv2.contourArea(c))
		counter += 1
		# print(counter)
		cv2.waitKey(0)

# mod abcde
	orig = oorig.copy()

	rabcde = abcde
	for approxa in abcde:
		for ppopints in range(1,len(approxa),2):
			approxa[-ppopints][0][0]=approxa[ppopints][0][0]


# abcde
	approxac=0
	for approxa in abcde:
		for popints in range(1,len(approxa)//2):
			# print(approxa[popints], approxa[-popints])
			# print(popints)
			# print("len(approxa//2:",len(approxa)//2)
			# dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
			dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
			dimA = dA / pixelsPerMetric

			cv2.line(orig, approxa[popints][0]+[x1,y1], approxa[-popints][0]+[x1,y1], (0, 255, 0), 1)
			cv2.circle(orig, approxa[popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			# if popints!=1:
				# continue
			cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10+4),
						# (int(tl[0])-15, int(tl[1])-15), 
						approxa[popints][0]+[x1,y1], 
						cv2.FONT_HERSHEY_SIMPLEX,
						0.35, (0, 255, 0), 1)
		approxac+=1

	cv2.imshow("Imager", orig)
	cv2.waitKey(0)
	
	cv2.destroyAllWindows()
In [ ]:
	cv2.destroyAllWindows()




###############################################################
# working on better contours
# plan to get local minimas and maximas
In [ ]:
# # for viewing all contours / debugging
# image2=roi
# contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
In [ ]:
# # for viewing all contours / debugging
image2=roi.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283d724e0>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(tcai))
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283da3ef0>
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1) # L
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1) # R
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x14283e066d8>
In [ ]:
cv2.destroyAllWindows()

# cutout(bu, bd)
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
origs = roi.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283e99320>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+500
In [ ]:
slice = origs[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=749'>750</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=750'>751</a> plt.imshow(imutils.opencv2matplotlib(slice))
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=751'>752</a> 

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
origs = oorig.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283f17320>
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+500
In [ ]:
slice = origs[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x14283f76588>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(maskt))
In [ ]:
y1=coordsr[0][1][1]
y2=coordsr[0][0][1]
x1=coordsr[0][0][0]
x2=coordsr[0][0][0]+50
In [ ]:
slice = origs[y1:y2, x1:x2]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428580d390>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(maskt))
In [ ]:
# # # for viewing all contours / debugging
# image2=roib.copy()
# contours, hierarchy= cv2.findContours(roib, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, 1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
In [ ]:
# # for viewing all contours / debugging
image2=roi.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x14285867860>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(tcai))
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
Out[ ]:
<matplotlib.image.AxesImage at 0x14285d18748>
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1) # L
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1) # R
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c513eb8>
In [ ]:
cv2.destroyAllWindows()

# cutout(bu, bd)
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c096160>
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1) # L
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1) # R
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c349860>
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=90'>91</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=91'>92</a> image = roi.copy()
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=92'>93</a> plt.imshow(imutils.opencv2matplotlib(image))

c:\Users\KshitijKoyande\Anaconda3\envs\pyis\lib\site-packages\imutils\convenience.py in opencv2matplotlib(image)
    128     # expects the image in RGB order, so simply convert from BGR
    129     # to RGB and return
--> 130     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    131 
    132 def url_to_image(url, readFlag=cv2.IMREAD_COLOR):

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))
mask3 = mask.copy()


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=98'>99</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=99'>100</a> # convert the image to grayscale
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=100'>101</a> gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=101'>102</a> plt.imshow(imutils.opencv2matplotlib(gray))
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=102'>103</a> # cv2.imshow("Gray", gray)

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=178'>179</a> # sort the contours from left-to-right and initialize the
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=179'>180</a> # 'pixels per metric' calibration variable
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=180'>181</a> (cnts, _) = contours.sort_contours(cnts)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=181'>182</a> 
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=182'>183</a> # TODO: needs to be fine tuned

AttributeError: 'tuple' object has no attribute 'sort_contours'
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
#imports
# from skimage.filters import threshold_local
# import argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import imutils
from imutils import perspective
from imutils import contours
In [ ]:
pixelsPerMetric = 60
In [ ]:
def midpoint(ptA, ptB): # to use in below function
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
In [ ]:
# # construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()


# ap.add_argument("-i", "--image", type=str, default="IMG_1097.JPG",
# 	help="path to input image")
# ap.add_argument("-w", "--width", type=float, default=10.0,
# 	help="width of the left-most object in the image (in inches)")

# args = vars(ap.parse_args())
In [ ]:
%matplotlib inline

# load the input image
# image = cv2.imread(args["image"]) 
# image = cv2.imread("frame126.jpg")

image = cv2.imread("IMG_1097.JPG")
image = imutils.resize(image, height = 500) # resize
oorig = image.copy()
plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
Out[ ]:
<matplotlib.image.AxesImage at 0x142854914e0>
In [ ]:
# Select ROI dynamically in real time

# r = cv2.selectROI(image)
# cv2.waitKey(0)

# # Crop image
# roi = image[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# a = int(r[1])
# b = int(r[1]+r[3])
# c = int(r[0])
# d = int(r[0]+r[2])
# print(f"{a}:{b}, {c}:{d}")
# cv2.waitKey(0)
# cv2.imshow("Cropped ROI", roi)
# cv2.destroyAllWindows()


# y1=a
# y2=b
# x1=c
# x2=d
# # %%
# cv2.destroyAllWindows()
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c6947f0>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))
mask3 = mask.copy()


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
tc # tile contours with rectangle border
Out[ ]:
[array([[224, 324],
        [491, 338],
        [488, 385],
        [222, 371]]),
 array([[231, 382],
        [479, 390],
        [478, 434],
        [229, 426]])]
In [ ]:
counter = 0

# image_tile = oorig
for t in tc:
	# print(c)
	image_tile = oorig
	roi_tile = image_tile[t[0][1]:t[2][1], t[0][0]:t[2][0]]

	cv2.imshow("roi_tile", roi_tile)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

	gray = cv2.cvtColor(roi_tile, cv2.COLOR_BGR2GRAY)
	# plt.imshow(imutils.opencv2matplotlib(gray))
	# cv2.imshow("Gray", gray)
	# plt.imshow(imutils.opencv2matplotlib(image))
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()

	# # %%

	# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
	# reducing high frequency noise
	blurred = cv2.GaussianBlur(gray, (11, 11), 0)
	# plt.imshow(imutils.opencv2matplotlib(blurred))



	# # %%

	# applying edge detection
	# edged = cv2.Canny(gray, 200, 255) 
	# edged = cv2.Canny(blurred, 100, 200) # blurred
	edged = cv2.Canny(blurred, 50, 100) # blurred
	# plt.imshow(imutils.opencv2matplotlib(edged))


	# # %%
	# # Autocanny 
	# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
	# edgeMap = imutils.auto_canny(blurred)
	# # cv2.imshow("Original", image)
	# plt.imshow(imutils.opencv2matplotlib(edgeMap))


	# # %%

	dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
	eroded = cv2.erode(dilated, None, iterations=2)
	# plt.imshow(imutils.opencv2matplotlib(eroded))


	# # %%

	# threshold the image by setting all pixel values less than 225 to 255 
	# (white; foreground) and all pixel values >= 225 to 255
	# (black; background), thereby segmenting the image 
	# in short binary inversion
	thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
	# plt.imshow(imutils.opencv2matplotlib(thresh))

	# # %%
	# we apply erosions to reduce the size of foreground objects
	# further erosion to lose some unwanted small spaces
	mask = thresh.copy()
	mask = cv2.erode(mask, None, iterations=1)
	# plt.imshow(imutils.opencv2matplotlib(mask))


	# # %%
	# find contours in the edge map
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	cnts = imutils.grab_contours(cnts)

	# sort the contours from left-to-right and initialize the
	# 'pixels per metric' calibration variable
	(cnts, _) = contours.sort_contours(cnts)

	orig = oorig.copy()
	countertemp = 0
	for c in cnts:
		
		# orig = oorig.copy()
		# if the contour is not sufficiently large, ignore it
		# print("Area of contour ", cv2.contourArea(c))
		if cv2.contourArea(c) < 100: # or cv2.contourArea(c) > 4000:
			continue
		
		# compute the rotated bounding box of the contour
		box = cv2.minAreaRect(c)
		box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
		box = np.array(box, dtype="int")
		# print(box)
	
		# order the points in the contour such that they appear
		# in top-left, top-right, bottom-right, and bottom-left
		# order, then draw the outline of the rotated bounding box
	
		# box = perspective.order_points(box)
		# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
	#or
		box = perspective.order_points(box)
		box = box.astype("int")+[t[0][0],t[0][1]] #[x1,y1]
		# asd
		# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
	
		# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
		# roic = [
		# 	[x1,y1],
		# 	[x2,y1],
		# 	[x2,y2],
		# 	[x1,y2]
		# 	]
		# roic = np.array(roic, dtype="int")
		# print(roic)
	
		cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
		# cv2.drawContours(orig, [box.astype("int")], -1, (255, 255, 0), 2)
		countertemp += 1
		# print("countertemp", countertemp)
	
		# print(box.astype("int"))
		# print(box.astype("int")+[x1, y1])
	
		# loop over the original points and draw them
		for (x, y) in box:
			cv2.circle(orig, (int(x), int(y)), 3, (0, 0, 255), -1)
	
		# unpack the ordered bounding box, then compute the midpoint
		# between the top-left and top-right coordinates, followed by
		# the midpoint between bottom-left and bottom-right coordinates
		(tl, tr, br, bl) = box
		(tltrX, tltrY) = midpoint(tl, tr)
		(blbrX, blbrY) = midpoint(bl, br)
		# compute the midpoint between the top-left and top-right points,
		# followed by the midpoint between the top-righ and bottom-right
		(tlblX, tlblY) = midpoint(tl, bl)
		(trbrX, trbrY) = midpoint(tr, br)

		cv2.line(orig, tl, bl, (0, 0, 255), 1) 
		cv2.line(orig, tr, br, (0, 0, 255), 1)

		# for approxa in abcde:
		# 	for popints in range(len(approxa//2)):
		# 		print(approxa[popints], approxa[-popints])
		# 		cv2.line(orig, approxa[popints], approxa[-popints], (255, 0, 0), 1)

	
		# # draw the midpoints on the image
		# cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
	
		# # draw lines between the midpoints
		# cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
		# 	(255, 0, 255), 2)
		# cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
		# 	(255, 0, 255), 2)
	
		# compute the Euclidean distance between the midpoints
		dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
		dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
	
		# if the pixels per metric has not been initialized, then
		# compute it as the ratio of pixels to supplied metric
		# (in this case, inches)
		if pixelsPerMetric is None:
			pixelsPerMetric = dB / 10 # args["width"]
			# print("pixelsPerMetric :",pixelsPerMetric)

	
		# compute the size of the object
		dimA = dA / pixelsPerMetric
		dimB = dB / pixelsPerMetric

	
		# draw the object sizes on the image
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tl[0])-15, int(tl[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tr[0])-15, int(tr[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		# cv2.putText(orig, "{:.2f}in".format(dimB),
		# 	(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
		# 	0.35, (0, 0, 255), 1)
	
		# show the output image
		# plt.imshow(imutils.opencv2matplotlib(orig))
		cv2.imshow("Imager", orig)
		# print("Area of contour ", cv2.contourArea(c))
		counter += 1
		# print(counter)
		cv2.waitKey(0)

# mod abcde
	orig = oorig.copy()

	rabcde = abcde
	for approxa in abcde:
		for ppopints in range(1,len(approxa),2):
			approxa[-ppopints][0][0]=approxa[ppopints][0][0]


# abcde
	approxac=0
	for approxa in abcde:
		for popints in range(1,len(approxa)//2):
			# print(approxa[popints], approxa[-popints])
			# print(popints)
			# print("len(approxa//2:",len(approxa)//2)
			# dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
			dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
			dimA = dA / pixelsPerMetric

			cv2.line(orig, approxa[popints][0]+[x1,y1], approxa[-popints][0]+[x1,y1], (0, 255, 0), 1)
			cv2.circle(orig, approxa[popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			# if popints!=1:
				# continue
			cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10+4),
						# (int(tl[0])-15, int(tl[1])-15), 
						approxa[popints][0]+[x1,y1], 
						cv2.FONT_HERSHEY_SIMPLEX,
						0.35, (0, 255, 0), 1)
		approxac+=1

	cv2.imshow("Imager", orig)
	cv2.waitKey(0)
	
	cv2.destroyAllWindows()
In [ ]:
	cv2.destroyAllWindows()




###############################################################
# working on better contours
# plan to get local minimas and maximas
In [ ]:
# # for viewing all contours / debugging
# image2=roi
# contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
In [ ]:
# # for viewing all contours / debugging
image2=roi.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c98a240>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(tcai))
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428cb12048>
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1) # L
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1) # R
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428cb6b5c0>
In [ ]:
cv2.destroyAllWindows()

# cutout(bu, bd)
In [ ]:
coordsl = []
coordsr = []
for i in range(n):
	# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
	# dimA = dA / pixelsPerMetric
	coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
	# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
	coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
	# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
In [ ]:
origs = oorig.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ca1eda0>
In [ ]:
origs = oorig.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ca86e80>
In [ ]:
y1s=coordsr[0][1][1]
y2s=coordsr[0][0][1]
x1s=coordsr[0][0][0]
x2s=coordsr[0][0][0]+50
In [ ]:
slice = origs[y1s:y2s, x1s:x2s]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428cae6f60>
In [ ]:
y1s=coordsr[0][1][1]
y2s=coordsr[0][0][1]
x1s=coordsr[0][0][0]
x2s=coordsr[0][0][0]+500
In [ ]:
slice = origs[y1s:y2s, x1s:x2s]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428d09db00>
In [ ]:
y1s=coordsr[0][1][1]
y2s=coordsr[0][0][1]
x1s=coordsr[0][0][0]
x2s=coordsr[0][0][0]+1
In [ ]:
slice = origs[y1s:y2s, x1s:x2s]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428d100ac8>
In [ ]:
y1s=coordsr[0][1][1]
y2s=coordsr[0][0][1]
x1s=coordsr[0][0][0]
x2s=coordsr[0][0][0]+5
In [ ]:
slice = origs[y1s:y2s, x1s:x2s]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428e9c3160>
In [ ]:
y1s=coordsr[0][1][1]
y2s=coordsr[0][0][1]
x1s=coordsr[0][0][0]
x2s=coordsr[0][0][0]+2
In [ ]:
slice = origs[y1s:y2s, x1s:x2s]
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ea1d4e0>
In [ ]:
for j in range(n):
	y1s=coordsr[0][1][1]
	y2s=coordsr[0][0][1]
	x1s=coordsr[0][0][0]
	x2s=coordsr[0][0][0]+2
In [ ]:
	slice = origs[y1s:y2s, x1s:x2s]
	plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ebf2208>
In [ ]:
for j in range(1):
	y1s=coordsr[0][1][1]
	y2s=coordsr[0][0][1]
	x1s=coordsr[0][0][0]
	x2s=coordsr[0][0][0]+2
In [ ]:
	slice = origs[y1s:y2s, x1s:x2s]
	plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428ec55240>
In [ ]:
gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
edged = cv2.Canny(blurred, 50, 100) # blurred
dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=758'>759</a> cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=759'>760</a> cnts = imutils.grab_contours(cnts)
---> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=760'>761</a> (cnts, _) = contours.sort_contours(cnts)

AttributeError: 'tuple' object has no attribute 'sort_contours'
In [ ]:
origs = oorig.copy()
# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
plt.imshow(imutils.opencv2matplotlib(origs))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f3bef98>
In [ ]:
for j in range(1):
	y1s=coordsr[0][1][1]
	y2s=coordsr[0][0][1]
	x1s=coordsr[0][0][0]
	x2s=coordsr[0][0][0]+2
In [ ]:
	slice = origs[y1s:y2s, x1s:x2s]
	plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f048438>
In [ ]:
gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
edged = cv2.Canny(blurred, 50, 100) # blurred
dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=758'>759</a> cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=759'>760</a> cnts = imutils.grab_contours(cnts)
---> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=760'>761</a> (cnts, _) = contours.sort_contours(cnts)

AttributeError: 'tuple' object has no attribute 'sort_contours'
In [ ]:
	slice = origs[y1s:y2s, x1s:x2s]
	plt.imshow(imutils.opencv2matplotlib(slice))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f0b14e0>
In [ ]:
gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
edged = cv2.Canny(blurred, 50, 100) # blurred
dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=758'>759</a> cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=759'>760</a> cnts = imutils.grab_contours(cnts)
---> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=760'>761</a> (cnts, _) = contours.sort_contours(cnts)

AttributeError: 'tuple' object has no attribute 'sort_contours'
In [ ]:
gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
edged = cv2.Canny(blurred, 50, 100) # blurred
dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
# (cnts, _) = contours.sort_contours(cnts)
In [ ]:
# for viewing all contours / debugging
image2=orig
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f114a58>
In [ ]:
# for viewing all contours / debugging
image2=orig
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f4f9c50>
In [ ]:
# for viewing all contours / debugging
image2=orig
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (255,255,255),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f550eb8>
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (255,255,255),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f5d2358>
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f91c1d0>
In [ ]:
contours
Out[ ]:
(array([[[ 0, 48]],
 
        [[ 1, 48]]], dtype=int32),
 array([[[ 0, 22]],
 
        [[ 1, 22]]], dtype=int32))
In [ ]:
len(contours)
Out[ ]:
2
In [ ]:
cnts
Out[ ]:
(array([[[ 0, 50]],
 
        [[ 0, 60]],
 
        [[ 1, 60]],
 
        [[ 1, 50]]], dtype=int32),
 array([[[ 0, 24]],
 
        [[ 0, 46]],
 
        [[ 1, 46]],
 
        [[ 1, 24]]], dtype=int32),
 array([[[ 0,  0]],
 
        [[ 0, 20]],
 
        [[ 1, 20]],
 
        [[ 1,  0]]], dtype=int32))
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428f97e3c8>
In [ ]:
contours
Out[ ]:
(array([[[ 0, 48]],
 
        [[ 1, 48]]], dtype=int32),
 array([[[ 0, 22]],
 
        [[ 1, 22]]], dtype=int32))
In [ ]:
cnts
Out[ ]:
(array([[[ 0, 50]],
 
        [[ 0, 60]],
 
        [[ 1, 60]],
 
        [[ 1, 50]]], dtype=int32),
 array([[[ 0, 24]],
 
        [[ 0, 46]],
 
        [[ 1, 46]],
 
        [[ 1, 24]]], dtype=int32),
 array([[[ 0,  0]],
 
        [[ 0, 20]],
 
        [[ 1, 20]],
 
        [[ 1,  0]]], dtype=int32))
In [ ]:
gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
edged = cv2.Canny(blurred, 50, 100) # blurred
# dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
# eroded = cv2.erode(dilated, None, iterations=2)
# thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
# mask = thresh.copy()
# mask = cv2.erode(mask, None, iterations=1)
# cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# cnts = imutils.grab_contours(cnts)
# (cnts, _) = contours.sort_contours(cnts)
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x14290c1a6d8>
In [ ]:
contours
Out[ ]:
(array([[[ 0, 49]],
 
        [[ 1, 49]]], dtype=int32),
 array([[[ 0, 21]],
 
        [[ 1, 21]]], dtype=int32))
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428e9645f8>
In [ ]:
contours
Out[ ]:
(array([[[ 0, 49]],
 
        [[ 1, 49]]], dtype=int32),
 array([[[ 0, 21]],
 
        [[ 1, 21]]], dtype=int32))
In [ ]:
contours[0][0]
Out[ ]:
array([[ 0, 49]], dtype=int32)
In [ ]:
contours[0][1]
Out[ ]:
array([[ 1, 49]], dtype=int32)
In [ ]:
contours[0][1][0]
Out[ ]:
array([ 1, 49], dtype=int32)
In [ ]:
contours[0][1][0][0]
Out[ ]:
1
In [ ]:
contours[0][1][0][1]
Out[ ]:
49
In [ ]:
contours[0][1][1][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 contours[0][1][1][1]

IndexError: index 1 is out of bounds for axis 0 with size 1
In [ ]:
contours[0][1][0]
Out[ ]:
array([ 1, 49], dtype=int32)
In [ ]:
contours[0][1]
Out[ ]:
array([[ 1, 49]], dtype=int32)
In [ ]:
contours[0]
Out[ ]:
array([[[ 0, 49]],

       [[ 1, 49]]], dtype=int32)
In [ ]:
contours[0][1][1][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 contours[0][1][1][1]

IndexError: index 1 is out of bounds for axis 0 with size 1
In [ ]:
contours[0][1][0]
Out[ ]:
array([ 1, 49], dtype=int32)
In [ ]:
contours[0][1][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 contours[0][1][1]

IndexError: index 1 is out of bounds for axis 0 with size 1
In [ ]:
contours[0][1][0][1]
Out[ ]:
49
In [ ]:
contours[1][1][0][1]
Out[ ]:
21
In [ ]:
contours[1]
Out[ ]:
array([[[ 0, 21]],

       [[ 1, 21]]], dtype=int32)
In [ ]:
contours
Out[ ]:
(array([[[ 0, 49]],
 
        [[ 1, 49]]], dtype=int32),
 array([[[ 0, 21]],
 
        [[ 1, 21]]], dtype=int32))
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
x=contours[0][1][0][0]
y1=contours[0][1][0][1]
y2=contours[1][1][0][1]
dS = dist.euclidean((x, y1), (x, y2))
print(dS)
28.0
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
x=contours[0][1][0][0]
y1=contours[0][1][0][1]
y2=contours[1][1][0][1]
dS = dist.euclidean((x, y1), (x, y2))
print(dS)

dimS = dS / pixelsPerMetric
print(((dimA*2.54)*10))
28.0
8.466666666666667
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
x=contours[0][1][0][0]
y1=contours[0][1][0][1]
y2=contours[1][1][0][1]
dS = dist.euclidean((x, y1), (x, y2))
print(dS)

dimS = dS / pixelsPerMetric
print(((dimA*2.54)*10))
print("{:.2f}mm".format((dimA*2.54)*10))

	
		# draw the object sizes on the image
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tl[0])-15, int(tl[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
  File "<ipython-input-343-f17a035380b5>", line 19
    cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
    ^
IndentationError: unexpected indent
In [ ]:
# for viewing all contours / debugging
image2=slice
contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
x=contours[0][1][0][0]
y1=contours[0][1][0][1]
y2=contours[1][1][0][1]
dS = dist.euclidean((x, y1), (x, y2))
print(dS)

dimS = dS / pixelsPerMetric
print(((dimA*2.54)*10))
print("{:.2f}mm".format((dimA*2.54)*10))
28.0
8.466666666666667
8.47mm
In [ ]:
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		image2=slice
		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1=contours[0][1][0][1]
		y2=contours[1][1][0][1]
		dS = dist.euclidean((x, y1), (x, y2))
		print(dS)

		dimS = dS / pixelsPerMetric
		print(((dimS*2.54)*10))
		print("{:.2f}mm".format((dimS*2.54)*10))
30.0
12.7
12.70mm
In [ ]:
def bcd_lr(n=10,t=2, u, d):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1=contours[0][1][0][1]
		y2=contours[1][1][0][1]
		dS = dist.euclidean((x, y1), (x, y2))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1=contours[0][1][0][1]
		y2=contours[1][1][0][1]
		dS = dist.euclidean((x, y1), (x, y2))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
  File "<ipython-input-346-a4372224d254>", line 5
    def bcd_lr(n=10,t=2, u, d):
              ^
SyntaxError: non-default argument follows default argument
In [ ]:
def bcd_lr(u, d,n=10,t=2):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1=contours[0][1][0][1]
		y2=contours[1][1][0][1]
		dS = dist.euclidean((x, y1), (x, y2))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1=contours[0][1][0][1]
		y2=contours[1][1][0][1]
		dS = dist.euclidean((x, y1), (x, y2))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=810'>811</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=811'>812</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=812'>813</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, n, t)
     18                 # coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l
     19                 # coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
---> 20                 coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l
     21                 coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r
     22 

UnboundLocalError: local variable 'y1' referenced before assignment
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, ):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=810'>811</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=811'>812</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=812'>813</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t)
     44                 contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
     45 
---> 46                 x=contours[0][1][0][0]
     47                 y1c=contours[0][1][0][1]
     48                 y2c=contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
contours[0][1][0][0]
Out[ ]:
1
In [ ]:
contours[0][0][0][0]
Out[ ]:
0
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, ):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][1][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=810'>811</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=811'>812</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=812'>813</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t)
     44                 contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
     45 
---> 46                 x=contours[0][1][0][0]
     47                 y1c=contours[0][1][0][1]
     48                 y2c=contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, ):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=810'>811</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=811'>812</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=812'>813</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t)
     44                 contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
     45 
---> 46                 x=contours[0][0][0][0]
     47                 y1c=contours[0][1][0][1]
     48                 y2c=contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, ):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
()
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=812'>813</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t)
     45                 print(contours)
     46 
---> 47                 x=contours[0][0][0][0]
     48                 y1c=contours[0][1][0][1]
     49                 y2c=contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
#imports
# from skimage.filters import threshold_local
# import argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import imutils
from imutils import perspective
from imutils import contours
In [ ]:
pixelsPerMetric = 60
In [ ]:
def midpoint(ptA, ptB): # to use in below function
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
In [ ]:
# # construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()


# ap.add_argument("-i", "--image", type=str, default="IMG_1097.JPG",
# 	help="path to input image")
# ap.add_argument("-w", "--width", type=float, default=10.0,
# 	help="width of the left-most object in the image (in inches)")

# args = vars(ap.parse_args())
In [ ]:
%matplotlib inline

# load the input image
# image = cv2.imread(args["image"]) 
# image = cv2.imread("frame126.jpg")

image = cv2.imread("IMG_1097.JPG")
image = imutils.resize(image, height = 500) # resize
oorig = image.copy()
plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428d0dc588>
In [ ]:
# Select ROI dynamically in real time

# r = cv2.selectROI(image)
# cv2.waitKey(0)

# # Crop image
# roi = image[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
# a = int(r[1])
# b = int(r[1]+r[3])
# c = int(r[0])
# d = int(r[0]+r[2])
# print(f"{a}:{b}, {c}:{d}")
# cv2.waitKey(0)
# cv2.imshow("Cropped ROI", roi)
# cv2.destroyAllWindows()


# y1=a
# y2=b
# x1=c
# x2=d
# # %%
# cv2.destroyAllWindows()
In [ ]:
# #####################

y1=263 #a
y2=433 #b
x1=214 #c
x2=491 #d

#######################
In [ ]:
roi = image[y1:y2, x1:x2]
# roi = image[207:479, 214:494]
# roi = image[263:433, 214:491]
roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
In [ ]:
%matplotlib inline

image = roi.copy()
plt.imshow(imutils.opencv2matplotlib(image))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428c6cf978>
In [ ]:
# def imgmod(image):
	# # %%

# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(imutils.opencv2matplotlib(gray))
# cv2.imshow("Gray", gray)
# plt.imshow(imutils.opencv2matplotlib(image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # %%

# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
# reducing high frequency noise
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
plt.imshow(imutils.opencv2matplotlib(blurred))



# # %%

# applying edge detection
# edged = cv2.Canny(gray, 200, 255) 
# edged = cv2.Canny(blurred, 100, 200) # blurred
edged = cv2.Canny(blurred, 50, 100) # blurred
eroi = edged.copy()
plt.imshow(imutils.opencv2matplotlib(edged))


# # %%
# # Autocanny 
# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# edgeMap = imutils.auto_canny(blurred)
# # cv2.imshow("Original", image)
# plt.imshow(imutils.opencv2matplotlib(edgeMap))


# # %%

dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
eroded = cv2.erode(dilated, None, iterations=2)
plt.imshow(imutils.opencv2matplotlib(eroded))


# # %%

# threshold the image by setting all pixel values less than 225 to 255 
# (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image 
# in short binary inversion
thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
plt.imshow(imutils.opencv2matplotlib(thresh))

# # %%
# we apply erosions to reduce the size of foreground objects
# further erosion to lose some unwanted small spaces
mask = thresh.copy()
mask = cv2.erode(mask, None, iterations=1)
plt.imshow(imutils.opencv2matplotlib(mask))
mask3 = mask.copy()


# # %%

# # for viewing all contours / debugging
# image2=image
# contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))

	# return mask
In [ ]:
# mask = imgmod(image)
In [ ]:
# find contours in the edge map
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)

# TODO: needs to be fine tuned
# pixelsPerMetric = 60 # hard coded value
In [ ]:
# %matplotlib widget
orig = oorig.copy()
tc = []
tca = []
abcde = []

# loop over the contours individually
nooftiles=0
for c in cnts:
	
	# orig = oorig.copy()
	
	# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
	roic = [
        [x1,y1],
        [x2,y1],
        [x2,y2],
     	[x1,y2]
        ]
	roic = np.array(roic, dtype="int")
	# print(roic)

	# if the contour is not sufficiently large, ignore it
	# print("Area of contour ", cv2.contourArea(c))
	if cv2.contourArea(c) < 3000 or cv2.contourArea(c) > 5000:
		continue

	tca.append(c)


	# output = roi.copy()
	# cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	# (x, y, w, h) = cv2.boundingRect(c)
	# text = "original, num_pts={}".format(len(c))
	# cv2.putText(output, 
	# text, 
	# (x, y - 15), 
	# cv2.FONT_HERSHEY_SIMPLEX,
	# 0.4, 
	# (0, 255, 0), 
	# 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Original Contour", output)
	# cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	# for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
	eps=0.0228
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, eps * peri, True)
	abcde.append(approx)

	# # draw the approximated contour on the image
	# output = image.copy()
	# cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
	# text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
	# cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
	# 	0.4, (0, 255, 0), 2)
	# show the approximated contour image
	# print("[INFO] {}".format(text))
	# cv2.imshow("Approximated Contour", output)
	# cv2.waitKey(0)
# cv2.destroyAllWindows()












	# compute the rotated bounding box of the contour
	box = cv2.minAreaRect(c)
	box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
	box = np.array(box, dtype="int")
 
	# order the points in the contour such that they appear
	# in top-left, top-right, bottom-right, and bottom-left
	# order, then draw the outline of the rotated bounding box
 
	# box = perspective.order_points(box)
	# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 #or
	box = perspective.order_points(box)
	box = box.astype("int")+[x1,y1]
	# asd
	# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
 
 
	cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 
###########################################################
	# print(box.astype("int"))
	# print(box.astype("int")+[x1, y1])
 
	# loop over the original points and draw them
	for (x, y) in box:
		cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
	# unpack the ordered bounding box, then compute the midpoint
	# between the top-left and top-right coordinates, followed by
	# the midpoint between bottom-left and bottom-right coordinates
	(tl, tr, br, bl) = box
	# # print(box)
	# print("tl", tl)
	# print("tr", tr)
	# print("br", br)
	# print("bl", bl)

	# print(nooftiles)
	nooftiles+=1
	
	cv2.putText(orig, f"{nooftiles}",
                (int(tl[0])+15, int(tl[1])), 
				cv2.FONT_HERSHEY_SIMPLEX,
                0.75, (0, 0, 255), 2)

	cv2.imshow("Imager", orig)
	# print("Area of contour ", cv2.contourArea(c))
 
	tc.append(box)
	cv2.waitKey(0)

tcai = orig.copy()
cv2.destroyAllWindows()
In [ ]:
tc # tile contours with rectangle border
Out[ ]:
[array([[224, 324],
        [491, 338],
        [488, 385],
        [222, 371]]),
 array([[231, 382],
        [479, 390],
        [478, 434],
        [229, 426]])]
In [ ]:
counter = 0

# image_tile = oorig
for t in tc:
	# print(c)
	image_tile = oorig
	roi_tile = image_tile[t[0][1]:t[2][1], t[0][0]:t[2][0]]

	cv2.imshow("roi_tile", roi_tile)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

	gray = cv2.cvtColor(roi_tile, cv2.COLOR_BGR2GRAY)
	# plt.imshow(imutils.opencv2matplotlib(gray))
	# cv2.imshow("Gray", gray)
	# plt.imshow(imutils.opencv2matplotlib(image))
	# cv2.waitKey(0)
	# cv2.destroyAllWindows()

	# # %%

	# apply a Gaussian blur with a 7x7 kernel to the image to smooth it,
	# reducing high frequency noise
	blurred = cv2.GaussianBlur(gray, (11, 11), 0)
	# plt.imshow(imutils.opencv2matplotlib(blurred))



	# # %%

	# applying edge detection
	# edged = cv2.Canny(gray, 200, 255) 
	# edged = cv2.Canny(blurred, 100, 200) # blurred
	edged = cv2.Canny(blurred, 50, 100) # blurred
	# plt.imshow(imutils.opencv2matplotlib(edged))


	# # %%
	# # Autocanny 
	# # gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
	# edgeMap = imutils.auto_canny(blurred)
	# # cv2.imshow("Original", image)
	# plt.imshow(imutils.opencv2matplotlib(edgeMap))


	# # %%

	dilated = cv2.dilate(edged, None, iterations=2) # 10 is an interesting number
	eroded = cv2.erode(dilated, None, iterations=2)
	# plt.imshow(imutils.opencv2matplotlib(eroded))


	# # %%

	# threshold the image by setting all pixel values less than 225 to 255 
	# (white; foreground) and all pixel values >= 225 to 255
	# (black; background), thereby segmenting the image 
	# in short binary inversion
	thresh = cv2.threshold(eroded, 225, 255, cv2.THRESH_BINARY_INV)[1]
	# plt.imshow(imutils.opencv2matplotlib(thresh))

	# # %%
	# we apply erosions to reduce the size of foreground objects
	# further erosion to lose some unwanted small spaces
	mask = thresh.copy()
	mask = cv2.erode(mask, None, iterations=1)
	# plt.imshow(imutils.opencv2matplotlib(mask))


	# # %%
	# find contours in the edge map
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	cnts = imutils.grab_contours(cnts)

	# sort the contours from left-to-right and initialize the
	# 'pixels per metric' calibration variable
	(cnts, _) = contours.sort_contours(cnts)

	orig = oorig.copy()
	countertemp = 0
	for c in cnts:
		
		# orig = oorig.copy()
		# if the contour is not sufficiently large, ignore it
		# print("Area of contour ", cv2.contourArea(c))
		if cv2.contourArea(c) < 100: # or cv2.contourArea(c) > 4000:
			continue
		
		# compute the rotated bounding box of the contour
		box = cv2.minAreaRect(c)
		box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
		box = np.array(box, dtype="int")
		# print(box)
	
		# order the points in the contour such that they appear
		# in top-left, top-right, bottom-right, and bottom-left
		# order, then draw the outline of the rotated bounding box
	
		# box = perspective.order_points(box)
		# cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
	#or
		box = perspective.order_points(box)
		box = box.astype("int")+[t[0][0],t[0][1]] #[x1,y1]
		# asd
		# cv2.drawContours(orig, [box.astype("int")+[x1,y1]], -1, (0, 255, 0), 2)
	
		# roic = [[x1,y1],[x2,y1],[x2,y2],[x1,y2]]
		# roic = [
		# 	[x1,y1],
		# 	[x2,y1],
		# 	[x2,y2],
		# 	[x1,y2]
		# 	]
		# roic = np.array(roic, dtype="int")
		# print(roic)
	
		cv2.drawContours(orig, [roic.astype("int")], -1, (255, 255, 0), 2)
		# cv2.drawContours(orig, [box.astype("int")], -1, (255, 255, 0), 2)
		countertemp += 1
		# print("countertemp", countertemp)
	
		# print(box.astype("int"))
		# print(box.astype("int")+[x1, y1])
	
		# loop over the original points and draw them
		for (x, y) in box:
			cv2.circle(orig, (int(x), int(y)), 3, (0, 0, 255), -1)
	
		# unpack the ordered bounding box, then compute the midpoint
		# between the top-left and top-right coordinates, followed by
		# the midpoint between bottom-left and bottom-right coordinates
		(tl, tr, br, bl) = box
		(tltrX, tltrY) = midpoint(tl, tr)
		(blbrX, blbrY) = midpoint(bl, br)
		# compute the midpoint between the top-left and top-right points,
		# followed by the midpoint between the top-righ and bottom-right
		(tlblX, tlblY) = midpoint(tl, bl)
		(trbrX, trbrY) = midpoint(tr, br)

		cv2.line(orig, tl, bl, (0, 0, 255), 1) 
		cv2.line(orig, tr, br, (0, 0, 255), 1)

		# for approxa in abcde:
		# 	for popints in range(len(approxa//2)):
		# 		print(approxa[popints], approxa[-popints])
		# 		cv2.line(orig, approxa[popints], approxa[-popints], (255, 0, 0), 1)

	
		# # draw the midpoints on the image
		# cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
		# cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
	
		# # draw lines between the midpoints
		# cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
		# 	(255, 0, 255), 2)
		# cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
		# 	(255, 0, 255), 2)
	
		# compute the Euclidean distance between the midpoints
		dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
		dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
	
		# if the pixels per metric has not been initialized, then
		# compute it as the ratio of pixels to supplied metric
		# (in this case, inches)
		if pixelsPerMetric is None:
			pixelsPerMetric = dB / 10 # args["width"]
			# print("pixelsPerMetric :",pixelsPerMetric)

	
		# compute the size of the object
		dimA = dA / pixelsPerMetric
		dimB = dB / pixelsPerMetric

	
		# draw the object sizes on the image
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tl[0])-15, int(tl[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10),
			(int(tr[0])-15, int(tr[1])-15), cv2.FONT_HERSHEY_SIMPLEX,
			0.35, (0, 0, 255), 1)
		# cv2.putText(orig, "{:.2f}in".format(dimB),
		# 	(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
		# 	0.35, (0, 0, 255), 1)
	
		# show the output image
		# plt.imshow(imutils.opencv2matplotlib(orig))
		cv2.imshow("Imager", orig)
		# print("Area of contour ", cv2.contourArea(c))
		counter += 1
		# print(counter)
		cv2.waitKey(0)

# mod abcde
	orig = oorig.copy()

	rabcde = abcde
	for approxa in abcde:
		for ppopints in range(1,len(approxa),2):
			approxa[-ppopints][0][0]=approxa[ppopints][0][0]


# abcde
	approxac=0
	for approxa in abcde:
		for popints in range(1,len(approxa)//2):
			# print(approxa[popints], approxa[-popints])
			# print(popints)
			# print("len(approxa//2:",len(approxa)//2)
			# dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
			dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
			dimA = dA / pixelsPerMetric

			cv2.line(orig, approxa[popints][0]+[x1,y1], approxa[-popints][0]+[x1,y1], (0, 255, 0), 1)
			cv2.circle(orig, approxa[popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
			# if popints!=1:
				# continue
			cv2.putText(orig, "{:.2f}mm".format((dimA*2.54)*10+4),
						# (int(tl[0])-15, int(tl[1])-15), 
						approxa[popints][0]+[x1,y1], 
						cv2.FONT_HERSHEY_SIMPLEX,
						0.35, (0, 255, 0), 1)
		approxac+=1

	cv2.imshow("Imager", orig)
	cv2.waitKey(0)
	
	cv2.destroyAllWindows()
In [ ]:
	cv2.destroyAllWindows()




###############################################################
# working on better contours
# plan to get local minimas and maximas
In [ ]:
# # for viewing all contours / debugging
# image2=roi
# contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(image2, contours, -1, (0,255,0),1)
# plt.imshow(imutils.opencv2matplotlib(image2))
In [ ]:
# # for viewing all contours / debugging
image2=roi.copy()
contours, hierarchy= cv2.findContours(eroi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image2, contours, -1, (0,255,0),1)
plt.imshow(imutils.opencv2matplotlib(image2))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428cef36a0>
In [ ]:
# plt.imshow(imutils.opencv2matplotlib(tcai))
In [ ]:
# debug cell / contour approximation
# draw the shape of the contour on the output image, compute the
# bounding box, and display the number of points in the contour
for c in tca:
	output = roi.copy()
	cv2.drawContours(output, [c], -1, (0, 255, 0), 3)
	(x, y, w, h) = cv2.boundingRect(c)
	text = "original, num_pts={}".format(len(c))
	cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 255, 0), 2)

	# show the original contour image
	# print("[INFO] {}".format(text))
	cv2.imshow("Original Contour", output)
	cv2.waitKey(0)

	# to demonstrate the impact of contour approximation, let's loop
	# over a number of epsilon sizes
	for eps in np.linspace(0.001, 0.05, 10):
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, eps * peri, True)
		# draw the approximated contour on the image
		output = image.copy()
		cv2.drawContours(output, [approx], -1, (0, 255, 0), 3)
		text = "eps={:.4f}, num_pts={}".format(eps, len(approx))
		cv2.putText(output, text, (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX,
			0.4, (0, 255, 0), 2)
		# show the approximated contour image
		# print("[INFO] {}".format(text))
		cv2.imshow("Approximated Contour", output)
		cv2.waitKey(0)
	cv2.destroyAllWindows()
In [ ]:
#2b starts here segmentation to median
# for i in range((len(abcde)))
bu=[]
bd=[]
cu=[]
cd=[]
du=[]
dd=[]

for approxa in abcde:
	# for popints in range(1,len(approxa)//2):
	bu.append(approxa[1][0])
	bd.append(approxa[-1][0])
	cu.append(approxa[2][0])
	cd.append(approxa[-2][0])
	du.append(approxa[3][0])
	dd.append(approxa[-3][0])
	

		
############
In [ ]:
orig=oorig.copy()
In [ ]:
# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
# dimA = dA / pixelsPerMetric

cv2.line(orig, bu[0]+[x1,y1+10], bd[0]+[x1,y1-10], (0, 255, 0), 1)
cv2.line(orig, bu[0]+[x1-10,y1+10], bd[0]+[x1-10,y1-10], (0, 0, 255), 1)
cv2.line(orig, bu[0]+[x1+10,y1+10], bd[0]+[x1+10,y1-10], (255, 0, 0), 1)

# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
# if popints!=1:
	# continue
# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
# 			# (int(tl[0])-15, int(tl[1])-15), 
# 			bu[0]+[x1,y1], 
# 			cv2.FONT_HERSHEY_SIMPLEX,
# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
Out[ ]:
<matplotlib.image.AxesImage at 0x1428d199518>
In [ ]:
# ### try writing modular code
# def cutout(pu, pd, n=10, t=2):
# 	""" function for getting strips of various portions to calculate median"""
# 	coords=[]
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
coords = []
for i in range(n):
	cv2.line(orig, pu[0]+[x1,y1+15], pd[0]+[x1,y1-15], (0, 255, 0), 1)
	cv2.line(orig, pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25], (0, 0, 255), 1) # L
	cv2.line(orig, pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25], (255, 0, 0), 1) # R
	# cv2.line(orig, pu[0]+[x1-(n-3*t),y1+15], pd[0]+[x1-(n-3*t),y1-15], (0, 0, 255), 1)
	# cv2.line(orig, pu[0]+[x1+(n-3*t),y1+15], pd[0]+[x1+(n-3*t),y1-15], (255, 0, 0), 1)
	
	# cv2.circle(orig, bu[0]+[x1,y1], 3, (0, 255, 0), -1)
	# cv2.circle(orig, approxa[-popints][0]+[x1,y1], 3, (0, 255, 0), -1)
	# if popints!=1:
		# continue
	# cv2.putText(orig, "{:.2f}mm".format((bu[0])),
	# 			# (int(tl[0])-15, int(tl[1])-15), 
	# 			bu[0]+[x1,y1], 
	# 			cv2.FONT_HERSHEY_SIMPLEX,
	# 			0.35, (0, 255, 0), 1)
plt.imshow(imutils.opencv2matplotlib(orig))
# cv2.imshow("iamgljler",orig)
Out[ ]:
<matplotlib.image.AxesImage at 0x1428d1f1978>
In [ ]:
cv2.destroyAllWindows()

# cutout(bu, bd)
In [ ]:
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, ):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t)
     67                 slice = origs[y1s:y2s, x1s:x2s]
     68 
---> 69                 gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
     70                 blurred = cv2.GaussianBlur(gray, (11, 11), 0)
     71                 edged = cv2.Canny(blurred, 50, 100) # blurred

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=813'>814</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=814'>815</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t, oorig)
     67                 slice = origs[y1s:y2s, x1s:x2s]
     68 
---> 69                 gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
     70                 blurred = cv2.GaussianBlur(gray, (11, 11), 0)
     71                 edged = cv2.Canny(blurred, 50, 100) # blurred

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]

		print(slice)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
[[[  7   7   6]
  [ 10   8   5]]

 [[  8   9   9]
  [  6   7   8]]

 [[  8   9   8]
  [ 10  10   9]]

 [[  9  10   8]
  [ 11  10  10]]

 [[ 12   8   7]
  [ 12   9   8]]

 [[ 10  10   8]
  [  9   9   6]]

 [[ 11  10  10]
  [ 10   9   8]]

 [[ 10  10   9]
  [ 12   9  10]]

 [[ 10  10   9]
  [ 12   9  11]]

 [[ 10  11  10]
  [ 13  11  16]]

 [[ 11  10   8]
  [ 10  10  10]]

 [[ 11  11   6]
  [ 11  11   7]]

 [[ 11  10   8]
  [ 12  12   8]]

 [[  9  11   8]
  [ 12  10   7]]

 [[ 11  12  12]
  [ 12  10  10]]

 [[ 11  10  12]
  [ 13  13  12]]

 [[  9  10  11]
  [ 12  13  12]]

 [[ 13  12  12]
  [ 11  11  10]]

 [[ 14  14  15]
  [ 14  15  14]]

 [[ 15  15  11]
  [ 17  17  13]]

 [[ 14  16   8]
  [ 14  19  12]]

 [[ 49  47  32]
  [ 42  46  30]]

 [[ 63  65  54]
  [ 58  61  49]]

 [[101 102  95]
  [ 77  78  67]]

 [[101 102 109]
  [101 105 102]]

 [[ 89  92  93]
  [ 97 104  98]]

 [[ 97  98  93]
  [112 113 102]]

 [[132 132 124]
  [118 117 112]]

 [[129 130 127]
  [117 119 117]]

 [[113 113 112]
  [107 109 103]]

 [[105 105 105]
  [115 116 113]]

 [[ 92  95  92]
  [108 109 107]]

 [[ 96 100  91]
  [ 88  91  87]]

 [[ 85  91  81]
  [ 75  78  73]]

 [[ 81  83  81]
  [ 77  76  74]]

 [[ 88  89  85]
  [ 82  82  81]]

 [[ 86  87  89]
  [ 81  82  77]]

 [[ 81  82  74]
  [ 82  84  75]]

 [[ 70  78  73]
  [ 72  81  74]]

 [[ 79  82  80]
  [ 85  86  86]]

 [[ 81  80  79]
  [ 79  79  78]]

 [[ 87  83  82]
  [ 81  77  77]]

 [[ 74  77  75]
  [ 74  78  75]]

 [[ 66  70  68]
  [ 79  81  78]]

 [[ 54  65  60]
  [ 71  75  80]]

 [[ 85  88  98]
  [ 90  89  98]]

 [[ 88  99 118]
  [118 118 129]]

 [[ 70  79 109]
  [ 80  86 111]]

 [[ 15  21  36]
  [ 19  25  42]]

 [[  7   8   6]
  [  7   8   6]]

 [[  6   7   3]
  [  7   8   6]]

 [[  8   9   2]
  [  9   8   5]]

 [[  7   7   5]
  [  7   9   6]]

 [[  7   8   6]
  [  6   8   6]]

 [[ 10   8   6]
  [ 12   9   8]]

 [[  8   8   6]
  [  9   7   6]]

 [[ 10   9   6]
  [  8   9   4]]

 [[  6   7   3]
  [  8   9   4]]

 [[  5   8   4]
  [  7  10   6]]

 [[ 10  10   9]
  [  9  10   4]]

 [[ 11   9  10]
  [ 11  10  11]]]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
[]
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=818'>819</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=819'>820</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t, oorig)
     69 
     70                 print(slice)
---> 71                 gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
     72                 blurred = cv2.GaussianBlur(gray, (11, 11), 0)
     73                 edged = cv2.Canny(blurred, 50, 100) # blurred

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
[[[  7   7   6]
  [ 10   8   5]]

 [[  8   9   9]
  [  6   7   8]]

 [[  8   9   8]
  [ 10  10   9]]

 [[  9  10   8]
  [ 11  10  10]]

 [[ 12   8   7]
  [ 12   9   8]]

 [[ 10  10   8]
  [  9   9   6]]

 [[ 11  10  10]
  [ 10   9   8]]

 [[ 10  10   9]
  [ 12   9  10]]

 [[ 10  10   9]
  [ 12   9  11]]

 [[ 10  11  10]
  [ 13  11  16]]

 [[ 11  10   8]
  [ 10  10  10]]

 [[ 11  11   6]
  [ 11  11   7]]

 [[ 11  10   8]
  [ 12  12   8]]

 [[  9  11   8]
  [ 12  10   7]]

 [[ 11  12  12]
  [ 12  10  10]]

 [[ 11  10  12]
  [ 13  13  12]]

 [[  9  10  11]
  [ 12  13  12]]

 [[ 13  12  12]
  [ 11  11  10]]

 [[ 14  14  15]
  [ 14  15  14]]

 [[ 15  15  11]
  [ 17  17  13]]

 [[ 14  16   8]
  [ 14  19  12]]

 [[ 49  47  32]
  [ 42  46  30]]

 [[ 63  65  54]
  [ 58  61  49]]

 [[101 102  95]
  [ 77  78  67]]

 [[101 102 109]
  [101 105 102]]

 [[ 89  92  93]
  [ 97 104  98]]

 [[ 97  98  93]
  [112 113 102]]

 [[132 132 124]
  [118 117 112]]

 [[129 130 127]
  [117 119 117]]

 [[113 113 112]
  [107 109 103]]

 [[105 105 105]
  [115 116 113]]

 [[ 92  95  92]
  [108 109 107]]

 [[ 96 100  91]
  [ 88  91  87]]

 [[ 85  91  81]
  [ 75  78  73]]

 [[ 81  83  81]
  [ 77  76  74]]

 [[ 88  89  85]
  [ 82  82  81]]

 [[ 86  87  89]
  [ 81  82  77]]

 [[ 81  82  74]
  [ 82  84  75]]

 [[ 70  78  73]
  [ 72  81  74]]

 [[ 79  82  80]
  [ 85  86  86]]

 [[ 81  80  79]
  [ 79  79  78]]

 [[ 87  83  82]
  [ 81  77  77]]

 [[ 74  77  75]
  [ 74  78  75]]

 [[ 66  70  68]
  [ 79  81  78]]

 [[ 54  65  60]
  [ 71  75  80]]

 [[ 85  88  98]
  [ 90  89  98]]

 [[ 88  99 118]
  [118 118 129]]

 [[ 70  79 109]
  [ 80  86 111]]

 [[ 15  21  36]
  [ 19  25  42]]

 [[  7   8   6]
  [  7   8   6]]

 [[  6   7   3]
  [  7   8   6]]

 [[  8   9   2]
  [  9   8   5]]

 [[  7   7   5]
  [  7   9   6]]

 [[  7   8   6]
  [  6   8   6]]

 [[ 10   8   6]
  [ 12   9   8]]

 [[  8   8   6]
  [  9   7   6]]

 [[ 10   9   6]
  [  8   9   4]]

 [[  6   7   3]
  [  8   9   4]]

 [[  5   8   4]
  [  7  10   6]]

 [[ 10  10   9]
  [  9  10   4]]

 [[ 11   9  10]
  [ 11  10  11]]]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
[]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
j= 1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=818'>819</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=819'>820</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t, oorig)
     32                 print("j=",j)
     33                 origs = oorig.copy()
---> 34                 y1s=coordsr[0][j+1][1]
     35                 y2s=coordsr[0][j][1]
     36                 x1s=coordsr[0][0][0]

IndexError: list index out of range
In [ ]:
coordsr[0][j+1][1]
Out[ ]:
334
In [ ]:
coordsr[0][j+2][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 coordsr[0][j+2][1]

IndexError: list index out of range
In [ ]:
coordsr[0][j+1][1]
Out[ ]:
334
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
[[[  7   7   6]
  [ 10   8   5]]

 [[  8   9   9]
  [  6   7   8]]

 [[  8   9   8]
  [ 10  10   9]]

 [[  9  10   8]
  [ 11  10  10]]

 [[ 12   8   7]
  [ 12   9   8]]

 [[ 10  10   8]
  [  9   9   6]]

 [[ 11  10  10]
  [ 10   9   8]]

 [[ 10  10   9]
  [ 12   9  10]]

 [[ 10  10   9]
  [ 12   9  11]]

 [[ 10  11  10]
  [ 13  11  16]]

 [[ 11  10   8]
  [ 10  10  10]]

 [[ 11  11   6]
  [ 11  11   7]]

 [[ 11  10   8]
  [ 12  12   8]]

 [[  9  11   8]
  [ 12  10   7]]

 [[ 11  12  12]
  [ 12  10  10]]

 [[ 11  10  12]
  [ 13  13  12]]

 [[  9  10  11]
  [ 12  13  12]]

 [[ 13  12  12]
  [ 11  11  10]]

 [[ 14  14  15]
  [ 14  15  14]]

 [[ 15  15  11]
  [ 17  17  13]]

 [[ 14  16   8]
  [ 14  19  12]]

 [[ 49  47  32]
  [ 42  46  30]]

 [[ 63  65  54]
  [ 58  61  49]]

 [[101 102  95]
  [ 77  78  67]]

 [[101 102 109]
  [101 105 102]]

 [[ 89  92  93]
  [ 97 104  98]]

 [[ 97  98  93]
  [112 113 102]]

 [[132 132 124]
  [118 117 112]]

 [[129 130 127]
  [117 119 117]]

 [[113 113 112]
  [107 109 103]]

 [[105 105 105]
  [115 116 113]]

 [[ 92  95  92]
  [108 109 107]]

 [[ 96 100  91]
  [ 88  91  87]]

 [[ 85  91  81]
  [ 75  78  73]]

 [[ 81  83  81]
  [ 77  76  74]]

 [[ 88  89  85]
  [ 82  82  81]]

 [[ 86  87  89]
  [ 81  82  77]]

 [[ 81  82  74]
  [ 82  84  75]]

 [[ 70  78  73]
  [ 72  81  74]]

 [[ 79  82  80]
  [ 85  86  86]]

 [[ 81  80  79]
  [ 79  79  78]]

 [[ 87  83  82]
  [ 81  77  77]]

 [[ 74  77  75]
  [ 74  78  75]]

 [[ 66  70  68]
  [ 79  81  78]]

 [[ 54  65  60]
  [ 71  75  80]]

 [[ 85  88  98]
  [ 90  89  98]]

 [[ 88  99 118]
  [118 118 129]]

 [[ 70  79 109]
  [ 80  86 111]]

 [[ 15  21  36]
  [ 19  25  42]]

 [[  7   8   6]
  [  7   8   6]]

 [[  6   7   3]
  [  7   8   6]]

 [[  8   9   2]
  [  9   8   5]]

 [[  7   7   5]
  [  7   9   6]]

 [[  7   8   6]
  [  6   8   6]]

 [[ 10   8   6]
  [ 12   9   8]]

 [[  8   8   6]
  [  9   7   6]]

 [[ 10   9   6]
  [  8   9   4]]

 [[  6   7   3]
  [  8   9   4]]

 [[  5   8   4]
  [  7  10   6]]

 [[ 10  10   9]
  [  9  10   4]]

 [[ 11   9  10]
  [ 11  10  11]]]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
[]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
j= 1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=818'>819</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=819'>820</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t, oorig)
     32                 print("j=",j)
     33                 origs = oorig.copy()
---> 34                 y1s=coordsr[0][j+1][1]
     35                 y2s=coordsr[0][j][1]
     36                 x1s=coordsr[0][0][0]

IndexError: list index out of range
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
coordsr[0][j+1][1]
Out[ ]:
334
In [ ]:
coordsr[0]
Out[ ]:
[array([392, 395]), array([392, 334])]
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
[[[  7   7   6]
  [ 10   8   5]]

 [[  8   9   9]
  [  6   7   8]]

 [[  8   9   8]
  [ 10  10   9]]

 [[  9  10   8]
  [ 11  10  10]]

 [[ 12   8   7]
  [ 12   9   8]]

 [[ 10  10   8]
  [  9   9   6]]

 [[ 11  10  10]
  [ 10   9   8]]

 [[ 10  10   9]
  [ 12   9  10]]

 [[ 10  10   9]
  [ 12   9  11]]

 [[ 10  11  10]
  [ 13  11  16]]

 [[ 11  10   8]
  [ 10  10  10]]

 [[ 11  11   6]
  [ 11  11   7]]

 [[ 11  10   8]
  [ 12  12   8]]

 [[  9  11   8]
  [ 12  10   7]]

 [[ 11  12  12]
  [ 12  10  10]]

 [[ 11  10  12]
  [ 13  13  12]]

 [[  9  10  11]
  [ 12  13  12]]

 [[ 13  12  12]
  [ 11  11  10]]

 [[ 14  14  15]
  [ 14  15  14]]

 [[ 15  15  11]
  [ 17  17  13]]

 [[ 14  16   8]
  [ 14  19  12]]

 [[ 49  47  32]
  [ 42  46  30]]

 [[ 63  65  54]
  [ 58  61  49]]

 [[101 102  95]
  [ 77  78  67]]

 [[101 102 109]
  [101 105 102]]

 [[ 89  92  93]
  [ 97 104  98]]

 [[ 97  98  93]
  [112 113 102]]

 [[132 132 124]
  [118 117 112]]

 [[129 130 127]
  [117 119 117]]

 [[113 113 112]
  [107 109 103]]

 [[105 105 105]
  [115 116 113]]

 [[ 92  95  92]
  [108 109 107]]

 [[ 96 100  91]
  [ 88  91  87]]

 [[ 85  91  81]
  [ 75  78  73]]

 [[ 81  83  81]
  [ 77  76  74]]

 [[ 88  89  85]
  [ 82  82  81]]

 [[ 86  87  89]
  [ 81  82  77]]

 [[ 81  82  74]
  [ 82  84  75]]

 [[ 70  78  73]
  [ 72  81  74]]

 [[ 79  82  80]
  [ 85  86  86]]

 [[ 81  80  79]
  [ 79  79  78]]

 [[ 87  83  82]
  [ 81  77  77]]

 [[ 74  77  75]
  [ 74  78  75]]

 [[ 66  70  68]
  [ 79  81  78]]

 [[ 54  65  60]
  [ 71  75  80]]

 [[ 85  88  98]
  [ 90  89  98]]

 [[ 88  99 118]
  [118 118 129]]

 [[ 70  79 109]
  [ 80  86 111]]

 [[ 15  21  36]
  [ 19  25  42]]

 [[  7   8   6]
  [  7   8   6]]

 [[  6   7   3]
  [  7   8   6]]

 [[  8   9   2]
  [  9   8   5]]

 [[  7   7   5]
  [  7   9   6]]

 [[  7   8   6]
  [  6   8   6]]

 [[ 10   8   6]
  [ 12   9   8]]

 [[  8   8   6]
  [  9   7   6]]

 [[ 10   9   6]
  [  8   9   4]]

 [[  6   7   3]
  [  8   9   4]]

 [[  5   8   4]
  [  7  10   6]]

 [[ 10  10   9]
  [  9  10   4]]

 [[ 11   9  10]
  [ 11  10  11]]]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
[]
(array([[[ 0, 48]],

       [[ 1, 48]]], dtype=int32), array([[[ 0, 22]],

       [[ 1, 22]]], dtype=int32))
j= 1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=818'>819</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=819'>820</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t, oorig)
     32                 print("j=",j)
     33                 origs = oorig.copy()
---> 34                 y1s=coordsr[0][j+1][1]
     35                 y2s=coordsr[0][j][1]
     36                 x1s=coordsr[0][0][0]

IndexError: list index out of range
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

##  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)


		##### left
		origs = oorig.copy()
		y1s=coordsl[0][j+1][1]
		y2s=coordsl[0][j][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
j= 1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=815'>816</a> # %%
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=816'>817</a> bcd_lr(du, dd)
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=817'>818</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=818'>819</a> 
      <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=819'>820</a> 

d:\work\marley\marleyflask\test2jpnb copy.py in bcd_lr(u, d, x1, y1, x2, y2, n, t, oorig)
     32                 print("j=",j)
     33                 origs = oorig.copy()
---> 34                 y1s=coordsr[0][j+1][1]
     35                 y2s=coordsr[0][j][1]
     36                 x1s=coordsr[0][0][0]

IndexError: list index out of range
In [ ]:
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      1 # %%
----> 2 pu = u
      3 pd = d
      4 
      5 coordsl = []

NameError: name 'u' is not defined
In [ ]:
	pu = u
	pd = d

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      1 # %%
----> 2 pu = u
      3 pd = d
      4 
      5 coordsl = []

NameError: name 'u' is not defined
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
coordsr[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 coordsr[0]

IndexError: list index out of range
In [ ]:
coordsr
Out[ ]:
[]
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][j+1][1]
		y2s=coordsr[0][j][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      3         print("j=",j)
      4         origs = oorig.copy()
----> 5         y1s=coordsr[0][j+1][1]
      6         y2s=coordsr[0][j][1]
      7         x1s=coordsr[0][0][0]

IndexError: list index out of range
In [ ]:
coordsr[0][j+1][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=757'>758</a> coordsr[0][j+1][1]

IndexError: list index out of range
In [ ]:
coordsr[0]
Out[ ]:
[array([392, 395]), array([392, 334])]
In [ ]:
coordsr[0][j+1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 coordsr[0][j+1]

IndexError: list index out of range
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
coordsr[0][j+1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> 1 coordsr[0][j+1]

IndexError: list index out of range
In [ ]:
coordsr[0]
Out[ ]:
[array([392, 395]), array([392, 334])]
In [ ]:
coordsr[1]
Out[ ]:
[array([394, 395]), array([394, 334])]
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[j+1][0][1]
		y2s=coordsr[j][0][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     11         # print(slice)
     12 
---> 13         gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
     14         blurred = cv2.GaussianBlur(gray, (11, 11), 0)
     15         edged = cv2.Canny(blurred, 50, 100) # blurred

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
y1s
Out[ ]:
395
In [ ]:
y2s
Out[ ]:
395
In [ ]:
x1s
Out[ ]:
392
In [ ]:
x2s
Out[ ]:
394
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[j+1][0][1]
		y2s=coordsr[j][0][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     11         # print(slice)
     12 
---> 13         gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
     14         blurred = cv2.GaussianBlur(gray, (11, 11), 0)
     15         edged = cv2.Canny(blurred, 50, 100) # blurred

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
In [ ]:
y1s
Out[ ]:
395
In [ ]:
y2s
Out[ ]:
395
In [ ]:
coordsr[j+1][0][1]
Out[ ]:
395
In [ ]:
coordsr[j][0][1]
Out[ ]:
395
In [ ]:
coords
Out[ ]:
[]
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
coordsr[0][0][1]
Out[ ]:
395
In [ ]:
coordsr[0][1][1]
Out[ ]:
334
In [ ]:
coordsr[0][1][1]
Out[ ]:
334
In [ ]:
coordsr[1][1][1]
Out[ ]:
334
In [ ]:
coordsr[2][1][1]
Out[ ]:
334
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[j+1][1][1]
		y2s=coordsr[j][0][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
j= 9
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      3         print("j=",j)
      4         origs = oorig.copy()
----> 5         y1s=coordsr[j+1][1][1]
      6         y2s=coordsr[j][0][1]
      7         x1s=coordsr[0][0][0]

IndexError: list index out of range
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
coordsr[j+1][1][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=757'>758</a> coordsr[j+1][1][1]

IndexError: list index out of range
In [ ]:
coordsr[0][1][1]
Out[ ]:
334
In [ ]:
coordsr[1][1][1]
Out[ ]:
334
In [ ]:
coordsr[3][1][1]
Out[ ]:
334
In [ ]:
coordsr[4][1][1]
Out[ ]:
334
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[0][0][0]
		x2s=coordsr[0][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
j= 9
In [ ]:
	for k in range(n):
		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[0][0][0]
		x2s=coordsl[0][0][0]-t
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
dimSl
Out[ ]:
[0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335]
In [ ]:
coordsr
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([394, 395]), array([394, 334])],
 [array([396, 395]), array([396, 334])],
 [array([398, 395]), array([398, 334])],
 [array([400, 395]), array([400, 334])],
 [array([402, 395]), array([402, 334])],
 [array([404, 395]), array([404, 334])],
 [array([406, 395]), array([406, 334])],
 [array([408, 395]), array([408, 334])],
 [array([410, 395]), array([410, 334])]]
In [ ]:
coordsr[0][1][1]
Out[ ]:
334
In [ ]:
coordsr[1][1][1]
Out[ ]:
334
In [ ]:
coordsr[2][1][1]
Out[ ]:
334
In [ ]:
coordsr[0][0][0]
Out[ ]:
392
In [ ]:
coordsr[1][0][0]
Out[ ]:
394
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]+t
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     20         x=contours[0][0][0][0]
     21         y1c=contours[0][1][0][1]
---> 22         y2c=contours[1][1][0][1]
     23         dS = dist.euclidean((x, y1c), (x, y2c))
     24         # print(dS)

IndexError: tuple index out of range
In [ ]:
contours
Out[ ]:
(array([[[ 0, 48]],
 
        [[ 1, 48]],
 
        [[ 2, 48]],
 
        [[ 3, 48]],
 
        [[ 2, 48]],
 
        [[ 1, 48]]], dtype=int32),)
In [ ]:
contours[1][1][0][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=757'>758</a> contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
coordsl
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([390, 395]), array([390, 334])],
 [array([388, 395]), array([388, 334])],
 [array([386, 395]), array([386, 334])],
 [array([384, 395]), array([384, 334])],
 [array([382, 395]), array([382, 334])],
 [array([380, 395]), array([380, 334])],
 [array([378, 395]), array([378, 334])],
 [array([376, 395]), array([376, 334])],
 [array([374, 395]), array([374, 334])]]
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     20         x=contours[0][0][0][0]
     21         y1c=contours[0][1][0][1]
---> 22         y2c=contours[1][1][0][1]
     23         dS = dist.euclidean((x, y1c), (x, y2c))
     24         # print(dS)

IndexError: tuple index out of range
In [ ]:
contours
Out[ ]:
(array([[[ 0, 48]],
 
        [[ 1, 48]]], dtype=int32),)
In [ ]:
contours[1][1][0][1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=757'>758</a> contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
contours[0][1][0][1]
Out[ ]:
48
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]+1
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     20         x=contours[0][0][0][0]
     21         y1c=contours[0][1][0][1]
---> 22         y2c=contours[1][1][0][1]+1
     23         dS = dist.euclidean((x, y1c), (x, y2c))
     24         # print(dS)

IndexError: tuple index out of range
In [ ]:
	for k in range(n):
		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[j+1][0][0]
		x2s=coordsl[j][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     20         x=contours[0][0][0][0]
     21         y1c=contours[0][1][0][1]
---> 22         y2c=contours[1][1][0][1]
     23         dS = dist.euclidean((x, y1c), (x, y2c))
     24         # print(dS)

IndexError: tuple index out of range
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]+1
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     20         x=contours[0][0][0][0]
     21         y1c=contours[0][1][0][1]
---> 22         y2c=contours[1][1][0][1]+1
     23         dS = dist.euclidean((x, y1c), (x, y2c))
     24         # print(dS)

IndexError: tuple index out of range
In [ ]:
contours[1][1][0][1]+1
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
----> <a href='file:///d%3A/work/marley/marleyflask/test2jpnb%20copy.py?line=757'>758</a> contours[1][1][0][1]+1

IndexError: tuple index out of range
In [ ]:
contours[0][1][0][1]
Out[ ]:
48
In [ ]:
contours
Out[ ]:
(array([[[ 0, 48]],
 
        [[ 1, 48]]], dtype=int32),)
In [ ]:
len(contours)
Out[ ]:
1
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]+1
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
j= 9
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      6         y2s=coordsr[0][0][1]
      7         x1s=coordsr[j][0][0]
----> 8         x2s=coordsr[j+1][0][0]
      9         # # %%
     10         slice = origs[y1s:y2s, x1s:x2s]

IndexError: list index out of range
In [ ]:
	for j in range(n-1):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]+1
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
In [ ]:
	for k in range(n):
		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[j+1][0][0]
		x2s=coordsl[j][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)

		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
     18         # print(contours)
     19 
---> 20         x=contours[0][0][0][0]
     21         y1c=contours[0][1][0][1]
     22         y2c=contours[1][1][0][1]

IndexError: tuple index out of range
In [ ]:
	for k in range(n):
		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[j+1][0][0]
		x2s=coordsl[j][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
# def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n-1):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]+1
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
In [ ]:
	for k in range(n):
		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[j+1][0][0]
		x2s=coordsl[j][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
dimSl
Out[ ]:
[0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55]
In [ ]:
dimSr
Out[ ]:
[0.4166666666666667, 0.4166666666666667, 0.4166666666666667, 0.4]
In [ ]:
coordsl[j+1][0][0]
Out[ ]:
374
In [ ]:
coordsl[j][0][0]
Out[ ]:
376
In [ ]:
coordsl
Out[ ]:
[[array([392, 395]), array([392, 334])],
 [array([390, 395]), array([390, 334])],
 [array([388, 395]), array([388, 334])],
 [array([386, 395]), array([386, 334])],
 [array([384, 395]), array([384, 334])],
 [array([382, 395]), array([382, 334])],
 [array([380, 395]), array([380, 334])],
 [array([378, 395]), array([378, 334])],
 [array([376, 395]), array([376, 334])],
 [array([374, 395]), array([374, 334])]]
In [ ]:
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
# def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n-1):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
In [ ]:
	for k in range(n):
		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[j+1][0][0]
		x2s=coordsl[j][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
dimSr
Out[ ]:
[0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.4166666666666667]
In [ ]:
dimSl
Out[ ]:
[0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55, 0.55]
In [ ]:
	for k in range(n):
		print("k=",k)

		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[k+1][0][0]
		x2s=coordsl[k][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
k= 0
k= 1
k= 2
k= 3
k= 4
k= 5
k= 6
k= 7
k= 8
k= 9
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
d:\work\marley\marleyflask\test2jpnb copy.py in <module>
      7         y1s=coordsl[0][1][1]
      8         y2s=coordsl[0][0][1]
----> 9         x1s=coordsl[k+1][0][0]
     10         x2s=coordsl[k][0][0]
     11         # # %%

IndexError: list index out of range
In [ ]:
	for k in range(n-1):
		print("k=",k)

		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[k+1][0][0]
		x2s=coordsl[k][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
k= 0
k= 1
k= 2
k= 3
k= 4
k= 5
k= 6
k= 7
k= 8
In [ ]:
orig=oorig.copy()
t=2
n=10
pu=du
pd=dd
In [ ]:
# def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
In [ ]:
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []
In [ ]:
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))
In [ ]:
	for j in range(n-1):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
In [ ]:
	for k in range(n-1):
		print("k=",k)

		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[k+1][0][0]
		x2s=coordsl[k][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
k= 0
k= 1
k= 2
k= 3
k= 4
k= 5
k= 6
k= 7
k= 8
In [ ]:
dimSl
Out[ ]:
[0.45,
 0.43333333333333335,
 0.43333333333333335,
 0.45,
 0.4666666666666667,
 0.48333333333333334,
 0.5166666666666667,
 0.55,
 0.55]
In [ ]:
dimSr
Out[ ]:
[0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.4166666666666667]
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	# # %%
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	# # %%
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n-1):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
	# # %%
	for k in range(n-1):
		print("k=",k)

		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[k+1][0][0]
		x2s=coordsl[k][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
In [ ]:
bcd_lr(du, dd)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
k= 0
k= 1
k= 2
k= 3
k= 4
k= 5
k= 6
k= 7
k= 8
In [ ]:
def bcd_lr(u, d,x1=x1,y1=y1,x2=x2,y2=y2, n=10,t=2, oorig=oorig):
	# # %%
	pu = du
	pd = dd

	coordsl = []
	coordsr = []

	dimSr = []
	dimSl = []

	# # %%
	for i in range(n):
		# dA = dist.euclidean(rabcde[approxac][popints][0], rabcde[approxac][-popints][0])
		# dimA = dA / pixelsPerMetric
		# coordsl.append([pu[0]+[-(i*t),15], pd[0]+[-(i*t),-25]]) # l				
		# coordsr.append([pu[0]+[(i*t),15], pd[0]+[(i*t),-25]]) # r
		coordsl.append([pu[0]+[x1-(i*t),y1+15], pd[0]+[x1-(i*t),y1-25]]) # l				
		coordsr.append([pu[0]+[x1+(i*t),y1+15], pd[0]+[x1+(i*t),y1-25]]) # r

	# #  %%

	# cv2.line(maskt, coordsr[0][0], coordsr[0][1], (0, 0, 255), 1)
	# cv2.line(maskt, coordsr[0][0]+[1,0], coordsr[0][1]+[1,0], (0, 0, 255), 1)
	# plt.imshow(imutils.opencv2matplotlib(origs))


	# # %%
	for j in range(n-1):
		print("j=",j)
		origs = oorig.copy()
		y1s=coordsr[0][1][1]
		y2s=coordsr[0][0][1]
		x1s=coordsr[j][0][0]
		x2s=coordsr[j+1][0][0]
		# # %%
		slice = origs[y1s:y2s, x1s:x2s]
		# print(slice)

		gray = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSr.append(dimS)
	# # %%
	for k in range(n-1):
		print("k=",k)

		##### left
		origs = oorig.copy()
		y1s=coordsl[0][1][1]
		y2s=coordsl[0][0][1]
		x1s=coordsl[k+1][0][0]
		x2s=coordsl[k][0][0]
		# # %%
		slicek = origs[y1s:y2s, x1s:x2s]

		# print(slicek)
		gray = cv2.cvtColor(slicek, cv2.COLOR_BGR2GRAY)
		blurred = cv2.GaussianBlur(gray, (11, 11), 0)
		edged = cv2.Canny(blurred, 50, 100) # blurred

		contours, hierarchy= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		# print(contours)
		if len(contours)!=2:
			continue
		x=contours[0][0][0][0]
		y1c=contours[0][1][0][1]
		y2c=contours[1][1][0][1]
		dS = dist.euclidean((x, y1c), (x, y2c))
		# print(dS)

		dimS = dS / pixelsPerMetric
		# print(((dimS*2.54)*10))
		# print("{:.2f}mm".format((dimS*2.54)*10))
		dimSl.append(dimS)
	return(dimSr, dimSl)
In [ ]:
r,l=bcd_lr(du, dd)
j= 0
j= 1
j= 2
j= 3
j= 4
j= 5
j= 6
j= 7
j= 8
k= 0
k= 1
k= 2
k= 3
k= 4
k= 5
k= 6
k= 7
k= 8
In [ ]:
r
Out[ ]:
[0.43333333333333335,
 0.43333333333333335,
 0.43333333333333335,
 0.4166666666666667]
In [ ]:
l
Out[ ]:
[0.45,
 0.43333333333333335,
 0.43333333333333335,
 0.45,
 0.4666666666666667,
 0.48333333333333334,
 0.5166666666666667,
 0.55,
 0.55]